在MVC剃刀,你是怎么做到以下子布局定义的RenderSection? [英] In MVC Razor, how do you do a RenderSection defined below a sub-layout?

查看:92
本文介绍了在MVC剃刀,你是怎么做到以下子布局定义的RenderSection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个顶级_Layout.cshtml,看起来是这样的:

I have a top-level _Layout.cshtml that looks something like this:

<html>
<head>
    @RenderSection("Header", required: false)
</head>
<body>

    @RenderSection("LeftPane", required: false)
    @RenderSection("RightPane", required: false)
    @RenderBody()

</body>
</html>

然后我有两个子布局。一个定义只是LeftPane部分,其他的定义了一个LeftPane和RightPane。这些子布局称为_LeftPane.cshtml和_LeftPlusRightPane.cshtml,他们有布局设置为_Layout.cshtml。

Then I have two "sub-layouts." One defines just the LeftPane section, the other defines both a LeftPane and a RightPane. These sub-layouts are called _LeftPane.cshtml and _LeftPlusRightPane.cshtml, and they have Layout set to "_Layout.cshtml."

然后在每个视图.cshtml文件,我的布局设置为_LeftPane.cshtml或_LeftPlusRightPane.cshtml,取决于我想显示在页面上。

Then in each View .cshtml file, I set the Layout to either _LeftPane.cshtml or _LeftPlusRightPane.cshtml, depending on what I want to show up on the page.

这一切工作正常。问题是我在℃加入新的头部,头部&gt;该文件的部分。此部分未在子布局中定义,而是在实际的观。当我尝试查看的东西就这样,我得到的错误:

That all works fine. The problem is with the new "Header" section I've added in the <head> portion of the document. This section is not defined in the sub-layouts, but rather in the actual Views. When I try to view something this way, I get the error:

下列路段已确定,但尚未呈现为页面布局〜/查看/共享/ _LeftPlusRightPane.cshtml:头

我不希望呈现在sublayouts页眉部分,我想渲染它在_Layout.cshtml文件。我如何通过从低层次的视图定义页眉部分,通过子布局,到顶部_layout?

I don't want to render the Header section in the sublayouts, I want to render it up in the _Layout.cshtml file. How do I "pass through" the defined Header section from the low level view, through the sub-layouts, up to the top _Layout?

推荐答案

您可以嵌套布局。所以_Layout2有布局=_Layout.cshtml;

You can nest layouts. So _Layout2 has Layout = "_Layout.cshtml";

您也可以使用_ViewStart文件每个视图子文件夹指定该子文件夹不同的默认布局。

You can also use _ViewStart files in each of your View subfolders to specify a different default layout for that subfolder.

要穿越一节,你只是做这样的事情:

To "pass through" the section, you just do something like this:

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

这允许您通过内容链。

这篇关于在MVC剃刀,你是怎么做到以下子布局定义的RenderSection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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