ajax - 渲染两个局部视图

时间:2012-09-13 18:32:57

标签: jquery asp.net-mvc

在按钮上单击我使用jquery $ .ajax获取一些数据,调用controller action方法并返回强类型的局部视图。数据显示在表格中。

当数据可用时,我还需要渲染另一个强类型的局部视图(与resultSetView相同的模型),以显示resultSetView的页面导航按钮。

我该如何处理第二部分?

$.ajax({
            type: "POST",
            url: $form.attr('action'),
            data: $form.serialize(),
            error: function (xhr, status, error) {
                //do something about the error   
            },
            success: function (response) {
                $("#resultSetDiv").html(response);
                //need to reload pageNavigationDiv
            }
        });

标记就像

<div id="pageNavigation >
    @Html.Partial("_pageNavigationView")
</div>
<div id="resultSetDiv">
     @RenderSection("_resultSetView")
</div>

1 个答案:

答案 0 :(得分:0)

以您的操作方式发送JSON响应,格式如下

{
    "success": "true",
    "currentPage": "3",
    "totalPages": "12",
    "viewString": "<div>Somecontent</div>"
}

其中viewString中的值将是要加载到resultSetDiv的HTML标记。

success: function (response) {
          if(response.sucesss=="true")
          {
              $("#resultSetDiv").html(response.viewString);
              $("#yourPageNumber").html(response.currentPage);
          }
}

这个answer解释了如何在JSON中发送ViewResult