I have a multi-level hierarchical layout inheritance:
Layout.cshtml
MaterialLayout.cshtml // inherits Layout.cshtml
UserPanelLayout.cshtml // inherits MaterialLayout.cshtml
Index.cshtml // inherits UserPanelLayout.cshtml
I have defined a section in Layout.cshtml
say @RenderSection("Loader", false)
, and I want to define that section in UserPanelLayout.cshml
.
But I get this error:
The following sections have been defined but have not been rendered by the page at '/Views/Shared/MaterialLayout.cshtml': 'Loader'. To ignore an unrendered section call IgnoreSection("sectionName").
What is the proper way to use sections in a multi-level hierarchical layout inheritance?
A layout can optionally reference one or more sections, by calling RenderSection. Sections provide a way to organize where certain page elements should be placed.
You want to use sections when you want a bit of code/content to render in a placeholder that has been defined in a layout page. In the specific example you linked, he has defined the RenderSection in the _Layout. cshtml.
You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder. Razor Pages use the folder structure as a convention for routing requests.
To achieve this you need define the section in MaterialLayout.cshtml
like this:
@section Loader
{
@RenderSection("Loader", false)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With