I'm starting learning MVC4. I came across the two possibilities of creating a View based on a Razor template or a Master Page.
I would like to understand the practical differences between the two.
For now, I can see that if I create a View using a Master Page, I can override several sections. For example, if my Master defines a "left column" placeholder and a "body" placeholder I can not only define the body for a specific View, but I can also render contents in the "left column" section for example to display controls that are bound to the context in which the page is (from a search box to a stock quote viewer). Also, Master Pages cannot be defined by making use of Razor templates, which are much less verbose than other syntax (partially wrong: someone managed to hack this aspect).
With Razor Layouts, I can only define one contiguous block of the page that can be overridden by specific View, and I should use multiple layouts (breaking DRY) for little changes in other parts of the page. Is my previous statement correct or am I missing something?
Obviously I can render contents in any part of the page by making good use of jQuery, but that's another matter
You could use sections with Razor. Scott Gu blogged about them here: http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx
In your Layout you could define as many sections as you wish:
<div id="leftMenu">
@RenderSection("LeftMenu", required: false)
</div>
which you could override in your views:
@section LeftMenu {
<div>... here comes the left menu for this view ...</div>
}
You could also test whether a section has been defined in a view and if not provide some default content:
@if (IsSectionDefined("LeftMenu")) {
@RenderSection("LeftMenu")
}
else {
<div>Some default left menu</div>
}