Posting data back to a controller from a Partial View rendered by Ajax

半世苍凉 提交于 2019-12-11 10:22:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!