Posting data back to a controller from a Partial View rendered by Ajax
I am returning a Partial View from an Ajax call:
$(document)开发者_JAVA技巧.ready(function () {
$("#itemSubmitter").click(function (e) {
$.ajax({
url: '@Url.Action("GetShippingAddress", "Order")',
type: "POST",
cache: false,
success: function (data) {
$("#shoppingAddressWrapper").html(data);
}
});
});
});
This returns the View as expected. However, the Partial has several Textboxes with data already populated. The #shoppingAddressWrapper is within a Form Tag.
When I submit the Form, the values in the Textboxes on the partial are not part of the Request.Params collection.
Is this normal? Can you not return input boxes as part of Partial View rendered via Ajax call and then post that data to the server and retrieve the values?
C
I have found what the problem is. You need to clear the model state for the text boxes.
if (Request.IsAjaxRequest())
ModelState.Clear();
You can clear just the values you need, or all the values. The ajax mechanism will otherwise insert the old values back.
精彩评论