简体   繁体   中英

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?

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>
}

You define the section in a view and render it in the _Layout.cshtml.

In your layout (master) page place this:

 @RenderSection("head", false)

In your view page place this:

@section head {

PUT VIEW SPECIFIC CODE HERE
}

Here "head" is the name of section that you can define in your view page.

Its somewhat like ContentPlaceHolder that we use in asp.net webforms.

By using @rendersection in your _Layout file, you have control of the layout and order of sections in you main .cshtml code (lets say its called index.cshtml)

For instance if you have an @RenderSection("scripts", false) at the end of your _Layout file, then even if the "scripts" section is placed at the top of your index.cshtml files, it will be rendered at the bottom. Thus ensuring that all script sections are loaded in a consistant manner throughout your application.

If in the future you decided for some reason to move all the scripts to the head section, you can easily do this by just moving one line of code in the _Layout file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM