这个rendersection代码是什么意思?

时间:2022-09-06 14:34:49

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

我是Asp的初学者。净MVC3。谁能解释一下这段代码的含义:

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

On ScottGu's article:

ScottGu的文章:

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

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.

有一个RenderSection的例子,但是它定义了@section,然后使用@RenderSection。在这个例子中,section head是定义的,在这个例子中,同一个头部被呈现,这让我感到困惑。

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

渲染的作用是什么,我如何找到渲染的效果?

2 个解决方案

#1


39  

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).

“RenderSection()”helper方法的第一个参数指定要在布局模板的那个位置呈现的部分的名称。第二个参数是可选的,它允许我们定义我们要呈现的部分是否是必需的。如果某个部分是“必需的”,那么Razor会在运行时抛出一个错误,如果该部分没有在基于布局文件的视图模板中实现的话(这样可以更容易地跟踪内容错误)。

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.

因此,RenderSection所做的就是呈现模板/视图中定义的部分(而不是一般的_Layout)。在“实现视图模板中的“边栏”一节中,他进一步解释了如何实现一节。

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.

总之,你所拥有的是一个名为“head”的部分,它在一个视图中呈现一个名为“head”的部分,它是向下/嵌套的。

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.

编辑:查看http://blogs.msdn.com/b/marcinon/archive/2010/12/15/razor- nestede -layouts-and- redefinedsections.aspx,了解嵌套视图的含义——但请注意,本文已经发表一年多了。

MasterLayout:

MasterLayout:

@RenderSection("head", false)

SubLayout:

SubLayout:

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

Content:

内容:

@{
    Layout = "~/Views/_SubLayout.cshtml";
}
@section head
{
    <title>Content-Layout</title>
}

#2


16  

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

在视图中定义该节,并在_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.

这里的“head”是可以在视图页面中定义的部分的名称。

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

它有点像我们在asp.net webforms中使用的ContentPlaceHolder。

#1


39  

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).

“RenderSection()”helper方法的第一个参数指定要在布局模板的那个位置呈现的部分的名称。第二个参数是可选的,它允许我们定义我们要呈现的部分是否是必需的。如果某个部分是“必需的”,那么Razor会在运行时抛出一个错误,如果该部分没有在基于布局文件的视图模板中实现的话(这样可以更容易地跟踪内容错误)。

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.

因此,RenderSection所做的就是呈现模板/视图中定义的部分(而不是一般的_Layout)。在“实现视图模板中的“边栏”一节中,他进一步解释了如何实现一节。

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.

总之,你所拥有的是一个名为“head”的部分,它在一个视图中呈现一个名为“head”的部分,它是向下/嵌套的。

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.

编辑:查看http://blogs.msdn.com/b/marcinon/archive/2010/12/15/razor- nestede -layouts-and- redefinedsections.aspx,了解嵌套视图的含义——但请注意,本文已经发表一年多了。

MasterLayout:

MasterLayout:

@RenderSection("head", false)

SubLayout:

SubLayout:

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

Content:

内容:

@{
    Layout = "~/Views/_SubLayout.cshtml";
}
@section head
{
    <title>Content-Layout</title>
}

#2


16  

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

在视图中定义该节,并在_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.

这里的“head”是可以在视图页面中定义的部分的名称。

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

它有点像我们在asp.net webforms中使用的ContentPlaceHolder。