我的_Layout.cshtml
包含
Html.Action("GetCartList", "Shared");
ShoppingCartViewModel scvm = (ShoppingCartViewModel)ViewData["ShoppingCartViewModel"];
我的控制器
[ChildActionOnly]
public ShoppingCartViewModel GetCartList()
{
var results = new ShoppingCartViewModel
{
Message = "",
SomeOtherProperty = "Other"
};
ViewData["ShoppingCartViewModel"] = results;
return results;
}
问题是为什么scvm
总是为空?当控制器“被击中”时,这些值将成功加载到ViewData["ShoppingCartViewModel"]
我希望我能做些像..。
ShoppingCartViewModel scvm = Html.Action("GetCartList", "Shared");
发布于 2015-11-12 09:52:10
在子操作中,将ViewData设置为ParentActionViewContext
。
[ChildActionOnly]
public void GetCartList()
{
var results = new ShoppingCartViewModel
{
Message = "",
SomeOtherProperty = "Other"
};
ControllerContext.ParentActionViewContext.ViewData["ShoppingCartViewModel"]=results;
}
在你的版面上,
@Html.Action("GetCartList", "Home");
@{
var scvm = (ShoppingCartViewModel) ViewContext.ViewData["ShoppingCartViewModel"];
<div> @scvm.SomeOtherProperty </div>
}
https://stackoverflow.com/questions/33677704
复制相似问题