Differences between RenderBody and RenderSection in ASP.NET MVC

In my earlier blog, I wrote about _ViewStart.cshtml file and MVC Routing.
In this blog, I will write about RenderBody vs RenderSection in MVC.

@RenderBody() vs @RenderSection()

RenderBody() –

RenderBody() must be available in Layout view i.e. it is mandatory for Layout view
RenderBody() does not take any parameter. A @RenderBody() in Layout page look like this –

 @RenderBody()

RenderSection() –

RenderSection() method is optional.
RenderSection() takes the parameter as section name and a boolean parameter which is optional.
The first parameter is of string type which is the name of the section i.e. in layout pages, renders the content of a named section.
The second parameter is of a boolean type which says – In layout pages, renders the content of a named section and specifies whether the section is required.

A RenderSection() method look like this –

  @RenderSection(“sectioname1”, required: true)

You can read other blogs on MVC here.

Next blog on MVC

Leave a Comment

RSS
YouTube
YouTube
Instagram