... bunch of inputs and presentation log...">

Using pjax to submit a form

I have a form on my page that has the following code

<form class="form">
    ... bunch of inputs and presentation logic...
    <div>
        <input type="submit" class="btn btn-primary" id="submit_btn" value="Save Part"/>
        <a class="btn" href="/Part/CopyPart/?id=@Model.ID ">Copy Part</a>
        <a class="btn" href="/Part/Delete/?id=@Model.ID">Delete Part</a>
        <a class="btn" href="/Part/PartList/?selectedType=@Model.PartType">Return To Part List</a>
    </div>
    @Html.HiddenFor(model => model.ID)
    @Html.HiddenFor(model => model.Manufacturer)
    @Html.HiddenFor(model => model.DateCreated)
    @Html.HiddenFor(model => model.Manufacturer)
    @Html.HiddenFor(model => model.IsActive)
    @Html.HiddenFor(model => model.PartType)
</form>

and I'm trying to use pjax () to submit this form and update the content with some results. My js code is as follows.

$(function() {
    $('a').pjax({ container: "#update_panel", timeout: 2000 }).live('click', function() {});
    $("#submit_btn").click(function() {
        var form = $('#form');
        $.pjax({
            container: "#update_panel", 
            timeout: 2000,
            url: "@Url.Action("UpdatePart","Part")",
            data: form.serialize()
        });
    });
});

This code passes calls to my UpdatePart () action, but does it pass an empty model to the action? How can I populate the model with the contents of the form so that it all works?

+5
source share
3 answers

You reference the form using an identifier, but the form has a class and identifier. Try:

var form = $(".form");

or

<form id="form">
+3
source

I see in my docs: https://github.com/defunkt/jquery-pjax They have an easy way to do this:

<div id="pjax-container">
</div>

<form id="myform" pjax-container>
 ... all inputs/submit/buttons etc ....
</form>

<script>
$(document).on('submit', 'form[pjax-container]', function(event) {
  $.pjax.submit(event, '#pjax-container')
})
</script>

... , /... , .

+5

, , false :

$("#submit_btn").click(function() {
    var form = $('#form');
    $.pjax({
        container: "#update_panel", 
        timeout: 2000,
        url: "@Url.Action("UpdatePart","Part")",
        data: form.serialize()
    });

    return false; // <-- cancel the default event
});
+3

All Articles