Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested layouts for MVC5

I've seen a few posts on this topic:

Razor Nested Layouts with Cascading Sections

MVC 3 - Nested layouts - sections don't render in Areas

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. 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. 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".

like image 758
Tim Avatar asked Jan 09 '14 17:01

Tim


People also ask

Where is_ layout Cshtml?

By default, the layout file is placed in the Pages/Shared folder, but it can be placed anywhere in the application folder structure. Use of the _ViewStart file to centralise the location of the layout makes updating to the new location easy: @{ Layout = "/Themes/MyTheme/_Layout.

What is_ ViewStart Cshtml?

The _ViewStart. cshtml page is a special view page containing the statement declaration to include the Layout page. Instead of declaring the Layout page in every view page, we can use the _ViewStart page. When a View Page Start is running, the “_ViewStart. cshtml” page will assign the Layout page for it.

Can we use multiple layout in MVC?

Yes, we can use multiple Layout in ASP.Net MVC application. By default, Visual Studio adds a Layout page in a shared folder which can be used by other View pages if required. In some cases, we can have the requirement to use multiple shared layout pages in MVC application.

What is layout View in MVC?

The layout view allows you to define a common site template, which can be inherited in multiple views to provide a consistent look and feel in multiple pages of an application. The layout view eliminates duplicate coding and enhances development speed and easy maintenance.


1 Answers

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. 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)
}
like image 114
Daniel Liuzzi Avatar answered Oct 07 '22 00:10

Daniel Liuzzi