WHAT IS DIFFERENCE BETWEEN RENDERBODY AND RENDERSECTION IN ASP.NET MVC

Sachin Singh

Total Post:26

Points:182
Posted by  Sachin Singh
 889  View(s)
Ratings:
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.
  1. Anupam Mishra

    Post:135

    Points:949
    Re: What is difference between RenderBody and RenderSection in ASP.NET MVC

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!