Can I have/chain more than 1 _Layout pages in MVC 3? for a cshtml page?

前端 未结 3 2125
长情又很酷
长情又很酷 2021-02-19 00:37

I have a layout page and some pages that use it. But I want to implement a navigation control on some of the sub-pages that use it. So I want to use another nested layout page.

3条回答
  •  再見小時候
    2021-02-19 00:56

    You can chain pages just like you can Master Pages, in _ViewStart.cshtml you will see how the layout page is defined:

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

    You can just add this section in the _Layout.cshtml to reference another parent, or add it to an individual view to reference a different layout view.

    ScottGu has a post with more info on Razor Layouts

    I use this exact method for what you are talking about on some ecommerce sites, where you are in the checkout process I do not want any distractions (navigation, etc) within the view, so instead of having some special case if checkout in layout, I make a LayoutBrandingOnly and a Layout which "inherits" from it so I don't have to repeat all the branding html.

提交回复
热议问题