c# - MVC 局部 View 在发布后呈现为 View

标签 c# html asp.net-mvc asp.net-mvc-4 razor

美好的一天 我有一个索引页面,在 2 个选项卡上显示 2 个局部 View 。每个选项卡上都有一个提交按钮,当提交按钮被点击时,它会点击 Controller , Controller 会执行一些处理并返回该部分 View 的填充模型,但部分 View 会呈现为自己的 View 。

Index.chtml页面

...
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3" style="float:left">
    <div >
        <ul id="myTab" class="nav nav-tabs nav-stacked">
            <li class="active">
                <a href="#IdentifyPerson" data-toggle="tab" class=""><strong>Identify Person</strong></a>
            </li>
            <li>
                <a href="#PersonInformation" data-toggle="tab" class=""><strong>Person Information</strong></a>
            </li>
        </ul>
    </div>
</div>

<div class="tab-content col-lg-9 col-md-9 col-sm-9 col-xs-9">
    <div id="IdentifyPerson" class="tab-pane fade in active">
        @Html.Partial("IdentifyPerson")
    </div>
    <div id="PersonInformation" class="tab-pane fade">
        @Html.Partial("PersonInformation")
    </div>
</div>

HomeCntroller.cs页面

[AuthorizeUser]
   public ActionResult Index(string ActionValue)
   {
       return View();
   }

   [HttpGet]
   [AuthorizeUser]
   public ActionResult IdentifyPerson()
   {
       return View();
   }

   [HttpPost]
   [AuthorizeUser]
   public ActionResult IdentifyPerson(ViewModel_IdentifyPerson model)
   {
       // do populate model
       return View(model);
   }

关于解决这个问题有什么建议吗?

身份识别页面 Identify Person

个人信息页面 Person Information here

最佳答案

您可能正在使用 Form 而不是 ajax form。所以你需要使用 ajax 表单来保持在同一页面上。

IdentifyPerson标签中

@using (Ajax.BeginForm("IdentifyPerson", "Home", null, new AjaxOptions { UpdateTargetId = "IdentifyPerson" , InsertionMode= InsertionMode.Replace}))
{
    //Other stuff
    <input type="submit" value="Save" />
}

PersonInformation标签

@using (Ajax.BeginForm("IdentifyPerson", "Home", null, new AjaxOptions { UpdateTargetId = "PersonInformation", InsertionMode = InsertionMode.Replace }))
{
    //Other stuff
    <input type="submit" value="Save" />
}

你需要包含这个Js文件 <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>在你看来 并更改您的返回类型 PartialView在行动中

[HttpPost]
    [AuthorizeUser]
    public ActionResult IdentifyPerson(ViewModel_IdentifyPerson model)
    {
        // do populate model
        return PartialView(model);
    }

关于c# - MVC 局部 View 在发布后呈现为 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35864414/

相关文章:

c# - 呈现线程上发生未指定的错误。 (NotifyPartitionIsZombie)

javascript - 我在 Canvas 绘图中做错了什么?

c# - ASP MVC : How get roles from ApplicationUser

c# - MVVM ViewModel : Best practices? 规定的多个步骤

c# - MVC 和 Web Api 中的 Owin 身份验证

javascript - Jquery-Mobile:将数据附加到非 native 选择选项菜单

c# - 卡在 Razor 扩展上

c# - 将隐藏对象从 View 传递到 Controller

c# - WPF - 如何将文本框添加到包含 ItemsControl 的项目的面板

javascript - 如果 highcharts.js 没有数据,图表容器的最小高度