Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Layout VS MVC Master Page

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

like image 876
usr-local-ΕΨΗΕΛΩΝ Avatar asked Nov 10 '12 12:11

usr-local-ΕΨΗΕΛΩΝ


People also ask

What is MVC master page?

Master page is used to create a common layout for the web based application. In Master page we use Content Place Holder where we want to place other pages content. Similarly we use the concept of Master page in MVC. We create a View which will be common to every page.

Does MVC have master page?

In addition to supporting partial views, ASP.NET MVC also supports the ability to create "master page" templates that can be used to define the common layout and top-level html of a site.

What is MVC layout?

The layout view allows you to define a common site template, which can be inherited in multiple views to provide a consistent look and feel in multiple pages of an application. The layout view eliminates duplicate coding and enhances development speed and easy maintenance.

What is the equivalent of ASP NET Master pages in razor views?

Layout is similar to the master page in ASP.NET Web Form. Similar to master page, the Layouts may contain CSS, jQuery files and multiple views. Layout can keep the user interface elements and theme consistency throughout the application.


1 Answers

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>
}
like image 95
Darin Dimitrov Avatar answered Oct 07 '22 00:10

Darin Dimitrov