Take the 2-minute tour ×

I know how to make nested master pages in webforms but is it also possible in MVC2? I know how to do it with markup only though.

I would like to set it from code depending on a criteria. Is it possible with some code in the controller to define the nested master page and master page?

share|improve this question
Duplicate of stackoverflow.com/questions/947134/… –  Ahmad Jul 23 '10 at 9:26
Bit harsh, this is a valid question. SO is not a forum where posters are insulted for not falling in line. @Nyla. There is a wealth of information on nesting master pages in MVC 2. What requirements do you have that makes things different for you? –  David Neale Jul 23 '10 at 11:10
Thank you David. I am looking for how to set a nested master page from within my controller. –  Nyla Pareska Jul 23 '10 at 11:27
@Nyla: Does the linked question help you with what you need? If not, you'll need to edit this question to provide more detail about what you're trying to do. If it does, I'll go ahead and close this one. –  Bill the Lizard Jul 23 '10 at 12:32
@Bill, I changed my initial question as apparently it was not clear enough. Thank you for your time. –  Nyla Pareska Jul 23 '10 at 12:51

3 Answers 3

" Is it possible with some code in the controller to define the nested master page and master page?"

Nope. The default view engine only lets you define one level of MasterPages.

See: http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.view.aspx

share|improve this answer

Well there is no way of doing that in the controller, afaik. However what is stopping you from passing in a ViewState variable and quering that in your view to determine what your masterpage should be ?

Something that might help you: templated partial view

share|improve this answer

The child page will look like a regular child master page.

The view page will inherit the child page.

place 'Inherits="System.Web.Mvc.ViewMasterPage"' in the top line, and point the MasterPageFile to the parent master page.

The content placeholders will be chained: i.e., the top master page will have ContentPlaceHolder sections with an ID, and the subordinate master page will have a Content section with a ContentPlaceHolderID set to the ID of the superior ContentPlaceHolder in the parent page.

The key is that the view engine will substitute the innermost text into the next level up placeholder until all placeholders are filled with html content.

I am preparing the MVC Music Store sample application with this method and it works fine for me.

This allows the programmer to follow the Dont Repeat Yourself rule and not repeat code from one master page to another. You can define a top-level master page with site navigation and basic formatting common to all pages, and then use a child master page when there are differences in page rendering that are unique to a specific set of views rendered by a specific controller.

I know this is an old post, but the concept is worth commenting on even today.

I am using MVC2 because that is what is currently authorized on my computer at work.

share|improve this answer

Your Answer


