How do I put data in a Masterpage?
Asked Answered
D

3

8

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?

Doss answered 2/6, 2009 at 21:32 Comment(5)
Is there a valid scenario for doing this? In general view specific content should be rendered in the views, the master page should only provide common elements which fit around what the view is creating.Rotherham
Maybe I'm completely off here, but I thought I'll use a strongly typed view if I need to add things like currently logged in username etc. to the masterpage. How else would I do this if not through the ViewModel thats given to the page?Doss
Assuming you are using a standard authentication method, e.g. Forms Authentication, then can do that using <%= HttpContext.Current.User.Identity.Name %>Rotherham
No, I'm not using standard auth.Doss
I'm also curious to a valid answer (more than "views render view data"). I can think of quite a few scenarios. () data driven menus / navigation (common to all pages, not any one specific view) () various login / user status options (logged in or not, new messages, upcoming password expiry notification) *Rianna
F
10

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/

Furfuraceous answered 3/6, 2009 at 4:41 Comment(1)
The first link does not seem to work anymore. The new one should be http://www.asp.net/mvc/tutorials/passing-data-to-view-master-pages-csConvert
Q
3

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.

Quality answered 4/6, 2009 at 13:53 Comment(0)
V
0

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.

Vanny answered 26/1, 2011 at 18:4 Comment(1)
Do you have any code examples Wyatt? I'd love to how you managed that.Impartial

© 2022 - 2024 — McMap. All rights reserved.