Layout & Master Page in ASP.NET MVC 5 (edit)
It's an interesting debate topic -> passing view models to the master page / layout vs ViewBags.
I'm -hate- using ViewBags so I have view models for all my view-layers. Starting with the _layout.cshtml and upwards. Any view that uses a Layout .. well .. that view model just inherits the Layout .. or whatever view is below it .. and this is repeated until you hit the bottom level which is usually the _layout...
My RavenOverflow project has some sample code that shows this.
- This is the the layout view, defined.
- Here is the layout view model, defined.
- This is the view that uses a layout.
- Here is the view model, for that previous view.
- This is some controller code, that creates the model .. which is then passed into the View.
Personally I prefer the second approach as it allows to handle the menu independently from the main logic. By using child actions you could have an entirely separate lifecycle of the menu controller without the need to have a base view model for absolutely all views that use this masterpage. Inheritance just doesn't seem right for this situation but of course this doesn't mean that you should rule it out completely. Every scenario is specific and depending on the exact details (which you haven't provided for yours) there might different approaches.
Just don't think that if Html.Action is good for one scenario it will be good for all of them. There might be some project specific constraints which make inappropriate or maybe achieve this by some other approach.
There is no universal solution that will work in all situations. Otherwise there wouldn't be a need for programmers :-)