0

我正在尝试使用 ajax 用新数据刷新部分。我在javascript中使用了这个函数:

function replace{
    $.ajax({
        url: '@Url.Action("DoThing", "Controller"),
        type: "POST",
        datatype: "actionresult",
        async: false,
        data: {itemCode: $("#ComboBox-input").val(), unitCode: @(Model.UnitCode)}
    });
}

和控制器动作:

Public ActionResult DoThing(int itemCode, int unitCode)
{
    var aThing = new ExModel
                     {
                         ItemCode = itemCode,
                         UnitCode = unitCode
                     }
    return PartialView("_InPartial", aThing);
}

现在在调试期间,我使用发送的数据进入控制器操作,但我不知道如何使用返回的数据呈现局部视图。

4

1 回答 1

2

首先,您的部分必须位于可以在 js 中引用的元素中,如下所示:

<div id="myPartialDiv"><!-- here your partial --></div>

然后,删除数据类型(不是必需的)并添加将服务器响应(您呈现的部分)放入 div 的成功方法:

function replace{
   $.ajax({
       url: '@Url.Action("DoThing", "Controller")',
       type: "POST",
       async: false,
       data: {itemCode: $("#ComboBox-input").val(), unitCode: @(Model.UnitCode)},
       success: function(response){
           $('#myPartialDiv').html(response);
       }
    });
}

通常你不必自己做这件事,我想你会有你的理由。但通常你应该使用Ajax.ActionLinkAjaxOptions,你设置UpdateTargetId为 myPartialDiv。

于 2012-03-14T10:54:11.603 回答