@RenderSection in nested razor templates

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)