MVC3 introduced Razor’s Layout as the new heir of the master page. It is a little different, has section rendering instead of content place holders, but it is pretty similar and intuitive to work with.
On the other day I had to create a layout within a layout. The reason for it was we have a basic, cross-website design, where in each page we have content in tabs, and each method on the controller is related to a tab. To support navigation between tabs by Ajax and by direct access for a url, each page should be a layout it self, and each tab – a view. This view will be called from the controller.
I tried several ways to achieve it, but the best one I found was this:
- Create a MVC Layout Page has the master layout (“master layout”). Use “RenderBody()” where needed.
- Create a MVC View Page With Layout for the page, and set its “Layout” property to the “master layout” (“page layout”). Use “RenderBody()” where needed.
- Create MVC View Page and set its “Layout” property to the “page layout”.
- Now you can call a method that will return the page.
One word about sections: It is a nice way of timing you html rendering:
This mechanism ensures the all the dependencies were loaded in a correct order and the tab script can be executed.
Happy Coding :)