在MVC Razor视图中使用@RenderBody有什么意义? [英] What's the point of using @RenderBody in a MVC Razor view?
问题描述
在我的MVC4剃须刀布局中,我使用的是@RenderSection
,而且我不会打扰
使用@RenderBody
.
然后在视图中,我将所有内容都放置在各个部分中.
这让我想知道@RenderBody
的意义除了
使不想将内容分成几部分的人更容易进行操作. @RenderBody
和@RenderSection
内是否有其他东西?
@RenderBody
呈现不在任何命名节中的页面内容.如果全部内容的内容位于您定义的部分之一之内,没有任何意义.但是...
在Razor语法中,@RenderSection
替换母版页.它使您可以在页面的特定区域中划分出各个部分,然后使内容的主体自然地出现在放置@RenderBody
声明的任何位置.
假设您正在写发票. RenderBody
区域将是页面中包含发票行项目的那部分.大多数报告都是如此.总是有一个页眉节,一个正文节和一个页脚节.
大概地,在网页的适当区域中呈现部分可以使您获得适当的语义行为,例如以始终显示在页面底部的方式设置页脚的样式.
<footer>
@RenderSection("Footer", @<span>This is my footer!</span>)
</footer>
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
In my MVC4 razor layout I am using @RenderSection
and I don't bother
with @RenderBody
.
Then in the views I just place everything in sections.
This makes me wonder what's the point of @RenderBody
other than to
make it easier for people who don't want to put things in sections. Is there anything different from @RenderBody
and something inside a @RenderSection
?
@RenderBody
renders the content of your page that is not within any named sections. If all of your content is within one of your defined sections, there is no point. However...
In Razor syntax, @RenderSection
replaces 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 @RenderBody
declaration is placed.
Let's say you are writing an invoice. The RenderBody
area 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.
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://haacked.com/archive/2011/03/05/defining-default-content-for-a-razor-layout-section.aspx
这篇关于在MVC Razor视图中使用@RenderBody有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!