开发者

Nested master pages in ASP.NET MVC 2

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 c开发者_JAVA技巧riteria. Is it possible with some code in the controller to define the nested master page and master page?


" 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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜