What are Sections in ASP.NET MVC?

A section allow you to specify a region of content within a layout. It expects one parameter which is the name of the section. If you don’t provide that, an exception will be thrown. A section in a layout page can be defined by using the following code.

@section header{
    <h1>Header Content</h1>
}

You can render above defined section header on the content page as given below:

@RenderSection("header")

By default, sections are mandatory. To make sections optional, just provides the second parameter value as false, which is a Boolean value.

@RenderSection("header",false)

 Note: A view can define only those sections that are referred to in the layout page otherwise an exception will be thrown.

Tagged , . Bookmark the permalink.

Leave a Reply