What I see is a string Layout property. But how can I pass a model to layout explicitly?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- MVC-Routing,Why i can not ignore defaults,The matc
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
this is pretty basic stuff, all you need to do is to create a base view model and make sure ALL! and i mean ALL! of your views that will ever use that layout will receive views that use that base model!
in the _Layout.cshtml:
in the the Index (for example) method in the home controller:
the Index.cshtml:
i disagree that passing a model to the _layout is an error, some user info can be passed and the data can be populate in the controllers inheritance chain so only one implementation is needed.
obviously for more advanced purpose you should consider creating custom static contaxt using injection and include that model namespace in the _Layout.cshtml.
but for basic users this will do the trick
Seems like you have modeled your viewmodels a bit wrong if you have this problem.
Personally I would never type a layout page. But if you want to do that you should have a base viewmodel that your other viewmodels inherits from and type your layout to the base viewmodel and you pages to the specific once.
Why dont you just add a new Partial View with i's own specific controller passing the required model to the partial view and finally Render the mentioned partial view on your Layout.cshtml using RenderPartial or RenderAction ?
I use this method for showing the logged in user's info like name , profile picture and etc.
old question but just to mention the solution for MVC5 developers, you can use the
Model
property same as in view.The
Model
property in both view and layout is assosiated with the sameViewDataDictionary
object, so you don't have to do any extra work to pass your model to the layout page, and you don't have to declare@model MyModelName
in the layout.But notice that when you use
@Model.XXX
in the layout the intelliSense context menu will not appear because theModel
here is a dynamic object just likeViewBag
.