Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use sections properly in a layout hierarchy in ASP.NET Core MVC Razor pages?

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?

like image 555
mohammad rostami siahgeli Avatar asked Feb 07 '18 08:02

mohammad rostami siahgeli


People also ask

What is used to reference one or more sections in the layout page?

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.

What is the purpose of using sections in the layout page?

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.

Can you mix MVC and Razor pages?

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.


1 Answers

To achieve this you need define the section in MaterialLayout.cshtml like this:

@section Loader
{
    @RenderSection("Loader", false)
}
like image 173
Ashley Medway Avatar answered Sep 27 '22 18:09

Ashley Medway