How would you push strongly typed information in MVC to the master page? Would you have a ViewModelBase class that contains master page information and inherit from it for every view model, or is there a better approach?
Thanks :)
How would you push strongly typed information in MVC to the master page? Would you have a ViewModelBase class that contains master page information and inherit from it for every view model, or is there a better approach?
Thanks :)
Alex,
I think what you are asking is, "Where is my master page controller?"
Have a look at the following link. It explains how to create an "Application Controller," an abstract class that can be inherited by your other controllers, so that you only have to write the code once that pushes your needed master page data into the view.
Passing Data to View Master Pages:
http://www.asp.net/learn/MVC/tutorial-13-cs.aspx
Also, have a look at the following link, which explains how to implement Partial Views and Subcontrollers in ASP.NET MVC:
Partial Requests in ASP.NET MVC
http://blog.codeville.net/2008/10/14/partial-requests-in-aspnet-mvc/
That is exactly the approach that I use. Have a MasterViewData base class containing information that might be common to all pages and is used to render the master page (logged in user when not using built-in auth, page-level messages). All my other view data classes derive from it.
I also do what Robert mentions: I have a base controller class that overrides the View method, which actually handles putting some of the master page information into the viewdata classes.
I'm curious if there are other options, but this approach has definitely worked well for me.