ASP MVC-3:在发布帖子后更新AJAX表单数据的问题

pad*_*ike 4 ajax asp.net-mvc jquery

在提交后通过AJAX更新for时遇到以下问题.由于某种原因,返回的HTML上的一些隐藏字段没有被更新,这很奇怪,因为当我运行调试器时,它们似乎具有正确的值.

这是我表格的相关部分

<div id="itemPopUpForm">
    @{Html.EnableClientValidation();}
    @Html.ValidationSummary()
    <div id="formDiv">
        @{ Html.RenderPartial("ItemData", Model, new ViewDataDictionary() { { "Machines", ViewBag.Machines }, { "WarehouseList", ViewBag.WarehouseList }, { WebConstants.FORM_ID_KEY, @ViewData[WebConstants.FORM_ID_KEY] } }); }
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

然后局部视图包含这些隐藏字段,这些字段是未更新的字段

@using (Html.BeginForm("Index", "Item", FormMethod.Post, new { id = "frmItem", name = "frmItem" }))
{ 
    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.Item.SodID)
    @Html.HiddenFor(model => Model.Item.ItemID) //The itemID needs updating when an item is copied
    @Html.HiddenFor(model => model.Item.Delivery.DeliveryAddressID, new { @id = "delAddressID" }) 
Run Code Online (Sandbox Code Playgroud)

这是更新表单的javascript方法

function ajaxSave() {
        if (!itemValid()) return; 
        popup('ajaxSplash');
        $.ajax({
            type: "POST",
            url: '@Url.Action("Index")',
            data: $("#frmItem").serialize(),
            success: function (html) {
                console.log(html);
                $("#formDiv").html(html);
                initItemPage();
                alert("Item was saved successfully");
            },
            error: function () { popup('ajaxSplash'); onFailure(); }
        });
    }
Run Code Online (Sandbox Code Playgroud)

操作索引返回部分视图"ItemData",当我检查项目模型时,它确实具有正确的值,但是当我看到返回的html时,它仍然设置为0.

Dar*_*rov 5

如果您打算在POST操作中修改模型属性,请不要忘记先从ModelState中删除它,否则HTML帮助程序将在渲染时使用原始发布的值:

[HttpPost]
public ActionResult Index(MyViewModel model)
{
    // remove the value from modelstate
    ModelState.Remove("Item.ItemID");

    // update the value
    model.Item.ItemID = 2;   

    return PartialView(model);
}
Run Code Online (Sandbox Code Playgroud)