MVC3: Layouts, RenderBody, RenderPage, RenderSection

xpertlab-html5
How to Use The HTML5 Sectioning Elements
13th April 2014
html card
HTML – Coding Cards
26th April 2014
Show all

MVC3: Layouts, RenderBody, RenderPage, RenderSection

MVC3: Layouts, RenderBody, RenderPage, RenderSection

Layouts

You typically want to maintain a consistent look and feel across all of the pages within your web-site/application.ASP.NET 2.0 introduced the concept of “master pages” which helps enable this when using .aspx based pages or templates. Razor supports this concept with a feature called “layouts”- which allow us to define a common site template, and then inherit its look and feel across all the views/pages on our site.

RenderBody

The RenderBody method resides in the layout page. There can only be one RenderBody method per layout page. The RenderBody method indicates where view templates that are based on this master layout file should “fill in” the body content.

RenderPage

Layout pages can also contain content that can be filled by other pages on disk. This is achieved by using the RenderPage method. This method takes either one or two parameters. The first is the physical location of the file, the second is an optional array of objects that can be passed into the page.

RenderSection

Layout pages also have the concept of sections. A layout page can only contain one RenderBody method, but can have multiple sections. To create a section you use the RenderSection method. The difference between RenderSection and RenderPage is RenderPage reads the content from a file; whereas RenderSection runs code blocks you define in your content pages.

Sample:

@RenderSection(“header”)
By default, sections are mandatory. To make sections optional, just add the second parameter, which is a Boolean value.