Razor Nested Layouts with Cascading Sections

前端 未结 4 1934
一向
一向 2020-12-12 23:34

I have an MVC3 site using Razor as its view engine. I want my site to be skinnable. Most of the possible skins are similar enough that they can derive from a shared master

相关标签:
4条回答
  • 2020-12-12 23:49

    I'm not sure if this is possible in MVC 3 but in MVC 5 I am able to successfully do this using the following trick:

    In ~/Views/Shared/_Common.cshtml write your common HTML code like:

    <!DOCTYPE html>
    <html lang="fa">
    <head>
        <title>Skinnable - @ViewBag.Title</title>
    </head>
    <body>
    @RenderBody()
    </body>
    </html>
    

    In ~/Views/_ViewStart.cshtml:

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

    Now all you have to do is to use the _Common.cshtml as the Layout for all the skins. For instance, in ~/Views/Shared/Skin1.cshtml:

    @{
        Layout = "~/Views/Shared/_Common.cshtml";
    }
    
    <p>Something specific to Skin1</p>
    
    @RenderBody()
    

    Now you can set the skin as your layout in controller or view based on your criteria. For example:

        public ActionResult Index()
        {
            //....
            if (user.SelectedSkin == Skins.Skin1)
                return View("ViewName", "Skin1", model);
        }
    

    If you run the code above you should get a HTML page with both the content of Skin1.cshtml and _Common.cshtml

    In short, you'll set the layout for the (skin) layout page.

    0 讨论(0)
  • 2020-12-12 23:54
    @helper ForwardSection( string section )
    {
       if (IsSectionDefined(section))
       {
           DefineSection(section, () => Write(RenderSection(section)));
       }
    }
    

    Would this do the job ?

    0 讨论(0)
  • 2020-12-13 00:01

    This is in fact not possible today using the public API (other than using the section redefinition approach). You might have some luck using private reflection but that of course is a fragile approach. We will look into making this scenario easier in the next version of Razor.

    In the meantime, here's a couple of blog posts I've written on the subject:

    • http://blogs.msdn.com/b/marcinon/archive/2010/12/08/optional-razor-sections-with-default-content.aspx
    • http://blogs.msdn.com/b/marcinon/archive/2010/12/15/razor-nested-layouts-and-redefined-sections.aspx
    0 讨论(0)
  • 2020-12-13 00:02

    Not sure if this will help you, but I wrote some extension methods to help "bubble up" sections from within partials, which should work for nested layouts as well.

    Injecting content into specific sections from a partial view ASP.NET MVC 3 with Razor View Engine

    Declare in child layout/view/partial

    @using (Html.Delayed()) {
        <b>show me multiple times, @Model.Whatever</b>
    }
    

    Render in any parent

    @Html.RenderDelayed();
    

    See the answer link for more use-cases, like only rendering one delayed block even if declared in a repeating view, rendering specific delayed blocks, etc.

    0 讨论(0)
提交回复
热议问题