1

I can do this with straight forward posts to different controllers/actions. Say I had a list of orders and when select an order I get the order in edit mode, how would I do this with jquery(i.e. no post backs etc all on the one page) and post the edit and return to the list. I want to still use the data annotations etc for validation. Cheers.

user1102550
  • 543
  • 1
  • 7
  • 24

1 Answers1

0

I am assuming you are using Spring MVC. I have accomplished this by using the $.ajax() function and serializing the Spring-bound form with the jQuery Form Plugin. See example below:

Create a container <div id="container"></div> around your Spring MVC form.

    $.ajax({
        type: 'POST',
        url: actionUrl,
        data: $('#id_of_your_form_tag').serialize(),
        success: function(data) {
            $('#container').html(data);
        },
        async: false
    });
izilotti
  • 4,757
  • 1
  • 48
  • 55
  • Right this makes sense. What about rendering the form. I was looking at rendering a partial view with ajax like this link. Is that a recommended way of doing it. – user1102550 Dec 28 '12 at 19:20
  • Right this makes sense. What about rendering the form. I was looking at rendering a partial view with ajax like this. Is that a recommended way of doing it.

    $.get( '@Url.Action("details","user", new { id = Model.ID } )', function(data) { $('#detailsDiv').replaceWith(data); }); One thing about is how do I pass the id. To go back to my original example. I have a list of orders and the above code displays the edit form. How does the above jquery know the model.id to use the above example? See http://stackoverflow.com/questions/1570127/render-partial-view-using-jquery-in-asp-net-mvc
    – user1102550 Dec 28 '12 at 19:29
  • Out of curiosity I'm doing this on an Ipad how do I do a newline. Return just posts the comment :) – user1102550 Dec 28 '12 at 19:32
  • The example provided is to render the form. The actionUrl should be obtained from the form's "action" attribute. You can keep the model id as a hidden field in your form. – izilotti Dec 28 '12 at 19:45
  • I think I've got the right approach. I'll generate my list of orders with an ajax action link for each order with the order id passed in the routing param. Point that at a partial view for the order details and render that and use the original suggestion to post the form with ajax. – user1102550 Dec 28 '12 at 20:53