Nested layouts for MVC5

余生长醉 提交于 2019-12-03 01:59:55
Daniel Liuzzi

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)
}

Not sure if you still need help, but I'll answer anyways.

There RenderSection method takes the following parameters according to the MSDN Documentation:

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);

If the section "required" parameter is false, it will not give an error if that section is not included by a view.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!