Home > DeveloperSection > Forums > What is difference between RenderBody and RenderSection in ASP.NET MVC
Sachin Singh
Sachin Singh

Total Post:26

Points:182
Posted on    March-25-2016 12:23 AM

 ASP.NET MVC C#  ASP.Net  ASP.NET MVC 
Ratings:


 1 Reply(s)
 221  View(s)
Rate this:
Hi All,

In ASP.NET MVC Razor syntax, we will unable to understand the use of @RenderBody. My layout page is below:


Can anyone give me a proper explanation of these two.

Thank you.


Anupam Mishra

Total Post:135

Points:949
Posted on    March-25-2016 3:54 AM

Hi Sachin,

A RenderSection allow to specify a region of content within a layout. It expects one parameter which is the name of the section. If we do not 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>
}
we 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)
A view can define only those sections that are referred to in the layout page otherwise an exception will be thrown.

RenderBody:
 
RenderBody method exists in the Layout page to render child page/view. It is just like the ContentPlaceHolder in master page. A layout page can have only one RenderBody method.

@RenderBody()
For more details. CLICK HERE

Don't want to miss updates? Please click the below button!

Follow MindStick