asp.net-mvc 在 MVC Razor 视图中使用 @RenderBody 有什么意义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14322458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:32:24  来源:igfitidea点击:

What's the point of using @RenderBody in a MVC Razor view?

asp.net-mvcasp.net-mvc-3asp.net-mvc-4

提问by Robert Harvey

In my MVC4 razor layout I am using @RenderSectionand I don't bother with @RenderBody.

在我使用的 MVC4 razor 布局中@RenderSection,我不打扰@RenderBody.

Then in the views I just place everything in sections.

然后在视图中,我只是将所有内容放在部分中。

This makes me wonder what's the point of @RenderBodyother than to make it easier for people who don't want to put things in sections. Is there anything different from @RenderBodyand something inside a @RenderSection?

这让我想知道@RenderBody除了让不想把东西分成几部分的人更容易之外还有什么意义。有什么不同的@RenderBody东西和里面的东西@RenderSection吗?

回答by Robert Harvey

@RenderBodyrenders the content of your page that is not within any named sections.If allof your content is within one of your defined sections, there is no point. However...

@RenderBody呈现不在任何命名部分中的页面内容如果您的所有内容都在您定义的部分之一内,则没有意义。然而...

In Razor syntax, @RenderSectionreplaces master pages. It allows you to carve out sections of the page for particular areas, and then allow the main body of the content to emerge naturally wherever the @RenderBodydeclaration is placed.

在 Razor 语法中,@RenderSection替换母版页。它允许您为特定区域划分页面的各个部分,然后允许内容的主体在@RenderBody放置声明的任何地方自然出现。

Let's say you are writing an invoice. The RenderBodyarea would the that part of the page that contains your invoice line items. This is true of most reports; there's always a header section, a body section and a footer section.

假设您正在写发票。该RenderBody区域将是包含您的发票行项目的页面部分。大多数报告都是如此;总是有一个页眉部分、一个正文部分和一个页脚部分。

Presumably, rendering a section in the appropriate area of a web page allows you to obtain proper semantic behavior, such as styling the footer in a way that it always appears at the bottom of a page.

据推测,在网页的适当区域呈现一个部分可以让您获得适当的语义行为,例如以始终出现在页面底部的方式设置页脚的样式。

<footer>
  @RenderSection("Footer", @<span>This is my footer!</span>)
</footer>

http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvc3razor_topic2.aspx

http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvc3razor_topic2.aspx

http://haacked.com/archive/2011/03/05/defining-default-content-for-a-razor-layout-section.aspx

http://haacked.com/archive/2011/03/05/defining-default-content-for-a-razor-layout-section.aspx