【发布时间】:2014-10-31 16:42:29
【问题描述】:
我有一个普通视图(NV)和 2 个局部视图(PV1 和 PV2),PV2 嵌套在 PV1 中,而 PV1 在 NV 中。我遇到的问题是 PV1 和 PV2 都有一个名为“isSuccessful”的可选五音表,它被添加到视图包“ViewBag.IsSuccessful = isSuccessful;”中。在动作中,由于某种原因,当 PV1 将其“isSuccessful”设置为 true 时,PV2 的“isSuccessful”也与 true 绑定。这会导致 PV2 过早地运行一些 JavaScript 函数,从而导致其他问题。
代码(结构和示例)
在控制器中
public ActionResult NV(int id)
{
var model = GetNVModel(id);
return View(model);
}
public ActionResult PV1 (int pv1Id = 0, bool isSuccessful = false)
{
var model = GetPV1Model(id);
ViewBag.IsSuccessful = isSuccessful;
return PartialView(model);
}
public ActionResult PV2 (int pv2Id = 0, bool isSuccessful = false)
{
var model = GetPV2Model(id);
if(model == null){model = new PV2Model();}
ViewBag.IsSuccessful = isSuccessful;
return PartialView(model);
}
观点
NV.cshtml
@model NVModel
<div>
@if (Model != null && Model.PV1Id != null && Model.PV1Id > 0)
{<text>
@Html.Action("PV1", "ControlerName", new { PV1Id = Model.PV1Id, isSuccessful = true})
</text>}
</div>
PV1.cshtml
@model PV1Model
@{bool isSuccessful = (ViewBag.IsSuccessful != null ? (bool)ViewBag.IsSuccessful : false);}
<div>
@if (Model != null && Model.PV2Id != null && Model.PV2Id > 0)
{<text>
@Html.Action("PV2", "ControlerName", new { PV2Id = Model.PV2Id })
</text>}
</div>
<script>
@if (isSuccessful )
{<text>
function sayHello()
{console.log('Hello PV1');}
</text>}
</script>
PV2.cshtml
@model PV2Model
@{bool isSuccessful = (ViewBag.IsSuccessful != null ? (bool)ViewBag.IsSuccessful : false);}
<div>
@if (Model != null)
{<text>
@Html.TextBoxFor(model=> model.Message)
</text>}
</div>
<script>
@if (isSuccessful )
{<text>
$(document).ready(function(){
console.log($("#Message").val());
});
</text>}
</script>
我不明白为什么 PV1 的 ViewData/ViewBag 被传递给 PV2。我认为不传递所有父 ViewData/ViewBag 是使用“@Html.Action”而不是“@Html.Partial”的一部分。 我需要知道的是是否有一个参数我需要传递给“@Html.Action”或者一个web.config属性需要翻转,因为上面不是一次性的事情,这是我做大部分页面的方式,以使视图尽可能灵活。
我正在使用 ASP.NET MVC 4、C# 和 .NET 4.5
感谢您对此问题的任何帮助或线索。
【问题讨论】:
标签: c# .net asp.net-mvc asp.net-mvc-4 asp.net-mvc-partialview