【问题标题】:Partial view does not render on posting the data from another partial view从另一个局部视图发布数据时,局部视图不会呈现
【发布时间】:2014-08-27 12:31:40
【问题描述】:

我有一个视图“主页”,我在其中呈现 2 个局部视图 _P1 和 _p2。 以下是我在主页视图中的代码:

<div>
        <div>
            @Html.Partial("_P1")
        </div>

        <div>
            @Html.Partial("_p2")
        </div>
</div>

我在 _P1 上有一个文本框和一个提交按钮,在 _P2 上只有一个标签,当我单击提交时,我希望发布数据,但将用户重定向到包含 _P1 和 _P2 的同一视图。但是_P2在提交后没有被渲染。

_P1 上的代码:

@using (Ajax.BeginForm("_P1", "Home", new AjaxOptions(), new { Id = "_P1form" }))
{
    @Html.LabelFor(m=>m.Name)
    @Html.TextBoxFor(m=>m.Name)
}
<input type="button" value="Save Form Data" class="save-button" onclick="submitform();"/>

请帮忙。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    只需使用 viewbag。根据您所在的步骤和视图在控制器上设置它

    @if(!ViewBag.PostBack){
        @Html.Partial("_p2")
    }
    

    【讨论】:

      【解决方案2】:

      按照您的方式渲染第一个部分视图,但可以在单击按钮时渲染第二个部分视图。更新您的开始以使用以下代码。

      在 AjaxOptions 中可以执行以下操作

      @using (
         Ajax.BeginForm("**Action Name from your controller**"
                         , "**Controller Name**"
                         , new  AjaxOptions
                          { 
                            HttpMethod ="Get", *// this can be get or post*
                            InsertionMode = InsertionMode.Replace, *// this will replace the content of the div. you can use the others like InsertAfter", "InsertBefore", or "Replace"*
                            UpdateTargetId="**Name of the div where you want to write the content**"
                          }
                       )
      {
      
      <div id="**div to be replaced**">
      </div>
      
      }
      

      查看描述 ajax 选项方法可用的其他选项的 URL。AjaxOptions Class

      如果您希望显示进度指示器,有趣的是 onBegin 和 onComplete

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-22
        • 1970-01-01
        • 1970-01-01
        • 2014-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-01
        相关资源
        最近更新 更多