Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherited layout in ASP.NET MVC

I have default layout _Layout.cshtml for the most pages. However for some group of pages I would like to have slightly modified default layout. I know I could just copy that file a modified it a bit, but it would mean to duplicate the code and maintain two layout with 99% of same code.

I would like to inherit the layout from the default like this:

LayoutInherited.cshtml:

Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.BodyContentClassSpecial = "";

@section header{
<style>
    #body-content {
        width: 90%;
        margin: 0 auto;
    }
</style>

}

It is possible to do something like this?

like image 335
Tomas Kubes Avatar asked Oct 10 '16 14:10

Tomas Kubes


People also ask

What is the use of _layout Cshtml in MVC?

So, the _layout. cshtml would be a layout view of all the views included in Views and its subfolders. The _ViewStart. cshtml can also be created in the sub-folders of the View folder to set the default layout page for all the views included in that particular subfolder.

Why TempData is used in MVC?

What is TempData and How to Use in MVC? TempData is used to transfer data from the view to the controller, the controller to the view, or from an action method to another action method of the same or a different controller. TempData temporarily saves data and deletes it automatically after a value is recovered.

What is the use of _ViewStart Cshtml in MVC?

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.

What is RenderBody and RenderPage in MVC?

The RenderBody method indicates where view templates that are based on this master layout file should “fill in” the body content. Layout pages can also contain content that can be filled by other pages on disk. This is achieved by using the RenderPage method. This method takes either one or two parameters.


1 Answers

Yes, layout inheritance is possible. Essentially, you're just creating a layout that itself utilizes a layout, since layouts are just views, there's no issues with that.

You pretty much do it exactly as you described:

_SubLayout.cshtml

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@RenderBody()

A few things to keep in mind:

  1. The content of the sub-layout will be placed where you have @RenderBody in your base layout, just as the content of a view would be. Your sub-layout still needs its own @RenderBody to determine where the content of the view that utilizes it should be placed.

  2. Any sections defined as required in your base layout must be implemented in your sub-layout or Razor will raise an exception, just as if your view did not implement the section. For example:

    _Layout.cshtml

    @RenderSection("Foo", required: true)
    

    _SubLayout.cshtml

    @section Foo
    {
        <p>Foo</p>
    }
    
  3. If your view needs to be able to implement a section (required or not), the sub-layout must define it. For example, in the code above, any view using _SubLayout.cshtml would not be able to define a Foo section, because it would no longer exist. An exception would be raised if you tried. In order to allow that view to define that section you would have to do something like the following:

    _SubLayout.cshtml

    @section Foo
    {
        @RenderSection("Foo", required: true)
    }
    

    This defines the section for the purpose of the base layout and then allows the section to be defined by any view that uses this sub layout.

There's actually a post on my blog that goes into all this in much greater detail if you need it: http://cpratt.co/how-to-change-the-default-asp-net-mvc-themet/

like image 107
Chris Pratt Avatar answered Oct 20 '22 19:10

Chris Pratt