【问题标题】:In MVC 3 I have a View with 3 Partial Views. How do i get the 3rd partial view to display on the same page?在 MVC 3 中,我有一个带有 3 个局部视图的视图。如何让第三部分视图显示在同一页面上?
【发布时间】:2014-05-29 18:56:17
【问题描述】:

我有一个包含 3 个部分视图的视图。第一个部分视图有一个下拉列表。您从下拉列表中选择某些内容,然后第二个部分视图将在同一页面上的正下方加载。

然后我在第二个局部视图中有一个搜索表单 (html.BeginForm),当我提交表单时,我想在第二个局部视图下打开第三个局部视图。

第三个局部视图有一个剑道 ui 网格,它采用模型。

现在的问题是第三个局部视图正在呈现在不同的页面上。

查看:

 <section>
      <div id="searchpanel">
          @html.Partial("_1stPartial")
          <div id="2ndPartialDiv"></div>
          <div id="3rdPartialDiv"></div>
      </div>
 </section>

局部视图2:

<section>
  <div id="searchblock">
     <table>
       <tr>
         <td>
            @using (Ajax.BeginForm("Search", "ControllerName", new AjaxOptions {  updateTargetId = "3rdPartialDiv"}))
          <fieldset>
          <ol>
            <li></li>
            <li>
             <input type="submit" value="Search" id="btnSearch"/>
            </li>
          </ol>
          </fieldset>
        </td>
       </tr>
      </table>
  </div>
</section>

控制器:

public ActionResult Search(model)
{
   //fill searchresults

   return PartialView("_3rdPartial", searchresults);
}

【问题讨论】:

  • 您所说的“正在呈现在不同的页面上”是什么意思?
  • 你能把代码贴在视图里吗?您是否使用 AJAX 加载第三个视图?
  • 贾斯汀,它执行整页帖子,并将我的部分视图放在新页面上。

标签: c# jquery .net asp.net-mvc kendo-asp.net-mvc


【解决方案1】:

Html.BeginForm 将执行整页发布。我相信你所追求的是 Ajax.BeginForm。

http://msdn.microsoft.com/en-us/library/system.web.mvc.ajax.ajaxextensions.beginform(v=vs.118).aspx

例子:

    @using (Ajax.BeginForm("TheActionResultYouWantToInvokeThatWillReturnTheThirdView", "YourController", null, new AjaxOptions { UpdateTargetId = "theIdOfTheDivForTheThirdView", OnSuccess = "doFunctionIfYouNeedTo", OnFailure = "ShowPopUpErrorIfYouWant" }))
     {

     }

邮政编码添加编辑:

此标记无效,可能是 UpdateTargetId 找不到 Div 的原因。

<section>
      <div id="searchpanel">
          @html.Partial("_1stPartial")
          <div id="2ndPartialDiv"></div>
          <div id="3rdPartialDiv"></div>
      </div>
 </section>

查看属性的结束语音标记。

【讨论】:

  • 我也一直在尝试 Ajax.BeginForm。似乎总是在我的控制器中转到我的 ActionResult 并以相同的方式执行。
  • 从你的控制器返回一个 PartialView
  • 琼斯是对的,赛斯。如果您需要更多帮助,请发布一些代码。
  • 我正在从我的控制器返回一个局部视图。抱歉没有发布代码。我们的开发是在一个完全隔离的网络中完成的。我会尝试在这里为您提供一个示例。
  • 另一件事可能是您没有在表单的 AjaxOptions 中指定 UpdateTargetId,这也会用部分 IIRC 替换整个页面。
【解决方案2】:

我从您的问题中了解到的是,您正在从第一个 PartialView 提交,如果成功,您将显示第二个。这个也一样。如果从第二个PartialView 成功创建了POST,则您要显示第三个。

为什么不从客户端使用Ajax?

 $.ajax ({
   type:'POST'
   data: {},
   success: function(response){
       $('.specific_div_container_for_previous_partial').hide();
       $('.specific_div_container_for_partial').html(response.Html);
       $('.specific_div_container_for_partial').show();
   },
   error: function(){
       // whatever
   }
 });

在服务器端,您将使用PartialView 返回呈现的 html。要在variable 中渲染PartialView 并将其作为json 对象发送到客户端,请查看THIS

更新 - 如何在 jquery 中序列化表单:

请关注THIS

【讨论】:

  • 我想我也试过这个。我会在javascript中有一个表单提交事件吗?如何从这里将模型数据传递给控制器​​中的操作?我还使用 Ajax.BeginForm 吗?
  • 您的表单上可以有一个普通按钮( input type='button' )。如果你这样做,你应该在 jquery 中创建一个点击处理程序。在该单击处理程序上,您可以将表单字符串化和序列化 JSON.stringify($('form').serialize) 发送到数据变量中。顺便说一句:我已经更新了我的问题
猜你喜欢
  • 2014-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-22
  • 2012-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多