问题
I am returning a Partial View from an Ajax call:
$(document).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
回答1:
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.
来源:https://stackoverflow.com/questions/7457967/posting-data-back-to-a-controller-from-a-partial-view-rendered-by-ajax