在MVC Razor视图中使用@RenderBody有什么意义? [英] What's the point of using @RenderBody in a MVC Razor view?

查看:450
本文介绍了在MVC Razor视图中使用@RenderBody有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆