美好的一天 我有一个索引页面,在 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);
}
关于解决这个问题有什么建议吗?
最佳答案
您可能正在使用 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/