是否有可能使刀片部分可选? [英] Is it possible to make Razor sections optional?

查看:106
本文介绍了是否有可能使刀片部分可选?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个网页:

<body>
    @section SomeStuff {
        <span>This is a section I just addered</span>
    }

</body>

是否有可能为布局的的渲染这一部分,或者是违背了应如何在概念上工作。好像这将是能够在一个页面上不会呈现某些部分有用的(除非我思考这个错误)。

Is it possible for the layout to not render this section, or is that contrary to how this should work conceptually. Seems like it would be useful to be able to not render certain sections on a page (unless I'm thinking about this incorrectly).

编辑:

包括错误信息可能会有所帮助,当我把一节进入的主要的页面时,的布局的网页失败:下面的章节已确定,但尚未呈现为页面布局/Views/Layouts/_Layout1.cshtml:SomeStuff,仿佛这是逼着我来渲染页面或某事上的每个部分

Including the error message may be helpful, when I put a section into the main page, the layout page fails with: The following sections have been defined but have not been rendered for the layout page "/Views/Layouts/_Layout1.cshtml": "SomeStuff". As if it's forcing me to render every section on the page or something.

在otherwords,在Layout.cshtml,我的的通话@RenderSection,但index.html中我有一段名为 SomeStuff 定义。这是否合法?好像它迫使我呈现在页面中的所有部分,但是,似乎是部分应是可选的,不是吗?

In otherwords, in Layout.cshtml, I don't call @RenderSection, but in Index.html I have a section called SomeStuff defined. Is that legal? Seems like it's forcing me to render all sections in the page, but that seems like sections should be optional, no?

推荐答案

如果需要部分,可以指定。

you can specify if a section is required.

@RenderSection("SomeStuff", required: false)

如果你不渲染出来的观点,它不应该再错误,指出此处

if you don't render it out in a view, it shouldn't error then, noted here

<一个href=\"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

这篇关于是否有可能使刀片部分可选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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