1

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?

7
  • 1
    Duplicate of stackoverflow.com/questions/947134/… Commented Jul 23, 2010 at 9:26
  • 1
    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? Commented Jul 23, 2010 at 11:10
  • Thank you David. I am looking for how to set a nested master page from within my controller. Commented Jul 23, 2010 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. Commented Jul 23, 2010 at 12:32
  • @Bill, I changed my initial question as apparently it was not clear enough. Thank you for your time. Commented Jul 23, 2010 at 12:51

3 Answers 3

1

" 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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.