如何在布局页面中返回视图模型?

时间:2015-11-12 17:34:13

标签: asp.net-mvc

我的_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");

1 个答案:

答案 0 :(得分:2)

在您的子操作中,将ViewData设置为Glide.with(context).load(R.drawable.macarrones).into(imageView);

ParentActionViewContext

在你的布局中,

[ChildActionOnly]
public void GetCartList()
{
    var results = new ShoppingCartViewModel
    {
       Message = "",
       SomeOtherProperty = "Other"
    };
    ControllerContext.ParentActionViewContext.ViewData["ShoppingCartViewModel"]=results;  
}