asp.net-mvc 共享布局视图可以在 ASP.NET MVC 中有一个控制器吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18832124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can the shared layout view have a controller in ASP.NET MVC?
提问by Pinch
Can the shared layout view have a controller?
共享布局视图可以有控制器吗?
I need to pass it Model information from a Controller?
我需要从控制器传递它的模型信息吗?
Or am I missing something here?
还是我在这里遗漏了什么?
采纳答案by Pinch
In the controller:
在控制器中:
public PartialViewResult Menu()
{
var ChargeTypes = db.ChargeTypes.ToList();
return PartialView(ChargeTypes);
}
And then its partial view:
然后是它的局部视图:
@model IEnumerable<ProposalMaker.Models.ChargeType>
@foreach (var item in Model)
{
<li>@item.Name</li>
}
Then in the shared partial view
然后在共享的局部视图中
@{Html.RenderAction("Menu","ChargeType");}
Thanks for the tip SLaks!
感谢 SLaks 的提示!
回答by Colin Bacon
To pass information to the layout, you will need to use a base view model that is used by all your view models. Your layout can then take this base model.
要将信息传递给布局,您需要使用所有视图模型都使用的基本视图模型。然后您的布局可以采用此基本模型。
I have previously answered an SO question on this
我以前回答过一个关于这个的问题
Pass data to layout that are common to all pages
Which has a detailed example.
里面有详细的例子。

