@manhng

Welcome to my blog!

Layout & Master Page in ASP.NET MVC 5

April 27, 2021 18:23

Layout & Master Page in ASP.NET MVC 5 (edit)

Change Layout Page Dynamically In ASP.NET MVC 5 (c-sharpcorner.com)

ASP.NET MVC Model: Make a BaseViewModel for your Layouts | DanylkoWeb

c# - How to pass viewmodel to a layout/master page? - Stack Overflow

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.

GitHub - PureKrome/RavenOverflow: StackOverflow rip with a RavenDb database store

what is the best way to bind ViewModel data to master page in asp.net-mvc - Stack Overflow

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 :-)

asp.net mvc 5 - Mvc5 pass model to Layout Page - Stack Overflow

asp.net mvc 4 - Pass data to layout that are common to all pages - Stack Overflow

Categories

Recent posts