@RenderSection in nested razor templates

前端 未结 1 993
無奈伤痛
無奈伤痛 2021-01-03 18:04

My problem is I can\'t seem to use @RenderSection from a nested template when @RenderSection is defined in the base template. Currently, I have a

相关标签:
1条回答
  • 2021-01-03 18:37

    You need to specify the sections that are allowed to pass through in the middle template.

    BaseTemplate.cshtml

    <!DOCTYPE html>
    <html>
      <head>
        <title>@ViewBag.Title</title>
        @RenderSection("HeaderContent", false) @* The region of the header scripts (custom css) *@
      </head>
    <body>
      @RenderBody()
    </body>
    </html>
    

    EDIT

    your new child template

    @{
      Layout = "~/Views/Shared/BaseTemplate.cshtml";
    }
    @section HeaderContent {
      @RenderSection("HeaderContent", false)
    }
    @RenderBody()
    

    If you put the render section inside of a section from the base template, it will render that section in the correct place on the base template.


    View.cshtml -> uses MiddleLayout.cshtml as it's layout

    @section HeaderContent
    {
        <!-- header content that will now render -->
    }
    
    <!-- page content -->
    
    0 讨论(0)
提交回复
热议问题