How do you put a strongly typed object in ASP.NET MVC into the master page?
Do you have a ViewModelBase
class that contains master page information and inherit from it for every view model, or is there a better approach?
How do you put a strongly typed object in ASP.NET MVC into the master page?
Do you have a ViewModelBase
class that contains master page information and inherit from it for every view model, or is there a better approach?
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.
We use a similar base ViewData, particularly for large content-oriented public sites where you've got alot of common UI stuff going on.
The trick we use to inject the common bit is to use an ActionFilter to inject the MasterPageViewModel around the our controllers return. This is quite a bit cleaner than having a special controller class with an overridden view method as there are definitely places where you don't want/need it. And the whole composition over inheritance thing.
© 2022 - 2024 — McMap. All rights reserved.