【问题标题】:ajax - render two partial viewsajax - 渲染两个局部视图
【发布时间】:2012-09-13 18:32:57
【问题描述】:

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

当数据可用时,我还需要渲染另一个强类型分部视图(与 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>

【问题讨论】:

    标签: jquery asp.net-mvc


    【解决方案1】:

    使用您的操作方法以以下格式发送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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-17
      • 2018-01-16
      • 1970-01-01
      • 2010-10-19
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多