[英]Nested layouts for MVC5
I've seen a few posts on this topic: 我在这个主题上看了几篇帖子:
Razor Nested Layouts with Cascading Sections Razor嵌套布局与层叠部分
MVC 3 - Nested layouts - sections don't render in Areas MVC 3 - 嵌套布局 - 部分不在区域中渲染
And it always seems to be problematic. 它似乎总是有问题的。 However they are both pretty old so wondering if things have changed.
然而,他们都很老了,所以想知道事情是否有所改变。
Basically I have a master layout, and 3 different body templates based on what kind of page it is. 基本上我有一个主布局,以及3种不同的主体模板,基于它是什么类型的页面。 For examples sake:
例如:
_Layout.cshtml
<html lang="en">
<head>
</head>
<body style="padding: 50px 0;">
<header class="navbar navbar-default navbar-fixed-top" role="banner">
@Html.Partial("_MenuPartial")
</header>
<ol class="breadcrumbs">
@RenderSection("breadcrumbs", true);
</ol>
<section>
@RenderBody();
</section>
<footer class="navbar navbar-default navbar-fixed-bottom">
@Html.Partial("_FooterPartial")
</footer>
@Html.Partial("_ScriptInitPartial")
</body>
</html>
_LayoutForEdit.cshtml
<div class="panel panel-primary">
<div class="panel-body">
<div class="col-lg-2">
<ul class="nav nav-pills nav-stacked">
@RenderSection("tabs", true)
</ul>
</div>
<div class="col-lg-10">
<div class="tab-content">
@RenderBody()
</div>
</div>
</div>
<div class="panel-footer">
<button class="btn btn-primary" data-bind="enable: Entity.isValid, click: save">Save</button>
</div>
</div>
Now this renders fine when called. 现在,这在调用时呈现正常。 Almost.
几乎。
The rendering of sections must be in the child layout it seems. 部分的呈现必须在它看起来的子布局中。 If I try to put the breadcrumbs in the
_Layout.cshtml
, it will fail because _LayoutForEdit.cshtml
never rendered it. 如果我尝试将面包屑放在
_Layout.cshtml
,它将失败,因为_LayoutForEdit.cshtml
从未呈现它。 How can I fix this? 我怎样才能解决这个问题?
The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_LayoutForEdit.cshtml": "breadcrumbs".
I know it's an old question. 我知道这是一个老问题。 I thought I'd share this anyway in case anyone else runs into this (like I did).
无论如何,我想我会分享这个,以防其他人遇到这个(就像我一样)。
At the bottom of your child layout, you define a section with the same name as the section in the parent layout. 在子布局的底部,您可以定义一个与父布局中的部分同名的部分。 Inside of this section you simply put a
@RenderSection
, again specifying the same name as before. 在本节的内部,您只需添加一个
@RenderSection
,再次指定与之前相同的名称。 Once this is in place, you essentially have the child layout "bypass" content from pages, up to its parent layout: 一旦到位,您基本上将子布局从页面“绕过”内容,直到其父布局:
@section breadcrumbs {
@RenderSection("breadcrumbs", true)
}
Not sure if you still need help, but I'll answer anyways. 不确定你是否还需要帮助,但无论如何我都会回答。
There RenderSection method takes the following parameters according to the MSDN Documentation : RenderSection方法根据MSDN文档采用以下参数:
public HelperResult RenderSection( string name, bool required )
Parameters
name
Type: System.String
The section to render.
required
Type: System.Boolean
true to specify that the section is required; otherwise, false.
Change the call to: 将通话更改为:
@RenderSection("breadcrumbs", false); @RenderSection(“breadcrumbs”,false);
If the section "required" parameter is false, it will not give an error if that section is not included by a view. 如果“required”参数部分为false,则如果视图未包含该部分,则不会给出错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.