Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this code of rendersection mean?

I am a beginner in Asp.Net MVC3. Can anybody please explain what is meant by this code:

@section head {     @RenderSection("head", false) } 

On ScottGu's article:

http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

there is an example of RenderSection but it defines @section and then somewhere @RenderSection is used. In this case section head is defined and within that itself the same head is being rendered which confused me.

What does RenderSection do and how do I find what is being rendered here?

like image 915
Tim Tom Avatar asked May 26 '12 08:05

Tim Tom


People also ask

What is @RenderSection?

@RenderSection is used for injecting content in the defined section. It allows you to specify a region in Layout. Two steps are there to define @RenderSection in ASP.NET MVC.

What is the difference between RenderBody and RenderSection?

RenderBody() renders all the content of the child view which is not wrapped in the named section. RenderSection() renders only a part of the child view which is wrapped under the named section.

What is use of RenderBody RenderSection and RenderPage in MVC?

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. The following code illustrates how to render a footer section. RenderSection expects one parameter and that is the name of the section.

What is RenderSection in razor?

RenderSection(String) In layout pages, renders the content of the section named name . RenderSection(String, Boolean) In layout pages, renders the content of the section named name .


1 Answers

Scott wrote at one point

The first parameter to the “RenderSection()” helper method specifies the name of the section we want to render at that location in the layout template. The second parameter is optional, and allows us to define whether the section we are rendering is required or not. If a section is “required”, then Razor will throw an error at runtime if that section is not implemented within a view template that is based on the layout file (which can make it easier to track down content errors).

So, what RenderSection does, is rendering a section defined in the template/view (not the general _Layout). A little bit furtherdown under "Implementing the “SideBar” Section in our View Template" he explains how to implement a section.

So all in all, what you have is a section called "head" that renders a section called "head" in a view that's further down/nested.

Edit: have a look at http://blogs.msdn.com/b/marcinon/archive/2010/12/15/razor-nested-layouts-and-redefined-sections.aspx to see what I mean with nested views - but note that this article is over a year old now.

MasterLayout:

@RenderSection("head", false) 

SubLayout:

@{     Layout = "~/Views/_MasterLayout.cshtml"; } @section head {     @RenderSection("head") } 

Content:

@{     Layout = "~/Views/_SubLayout.cshtml"; } @section head {     <title>Content-Layout</title> } 
like image 173
Brunner Avatar answered Sep 22 '22 20:09

Brunner