@RenderSection in nested razor templates

前端 未结 1 1012
無奈伤痛
無奈伤痛 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

    
    
      
        @ViewBag.Title
        @RenderSection("HeaderContent", false) @* The region of the header scripts (custom css) *@
      
    
      @RenderBody()
    
    
    

    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
    {
        
    }
    
    
    

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