@RenderSection in nested razor templates

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 nested base template which is linked to a child template which is then used in the view pages. When I define the @RenderSection in the base template and render it in the view pages it throws an error.

Here’s the exact problem.

I want to create a RenderSection to allow me to insert custom scripts.
My base template….

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

</head>
<body>
@RenderBody()
</body>
</html>

I then skip the child template as I do not want to put any custom head code in there and apply it to the page itself..

@section HeaderContent {
    <script>alert("hi");</script>
}

My problem is that I cant seem to add custom head code in to the base template from my normal pages.

The following sections have been defined but have not been rendered for the layout page ~/Views/Shared/OneColLayer.cshtml": "HeaderContent.

Do I need to include a pointer to the base template in the view page?

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

My new base template

<head>
  <link rel="stylesheet" type="text/css" href="@Url.Content(" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"~/content/layout.css")" />
  <link rel="stylesheet" type="text/css" href="@Url.Content(" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"~/content/global.css")" />
  <script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")"></script>
  <script type="text/javascript" src="@Url.Content("~/js/fadeInFadeOut.js")"></script>
  <title>@ViewBag.Title</title>
  @RenderSection("HeaderContent", false)
</head>
<body>
  @RenderBody()
</body>

my new child template

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

my view

@{
  ViewBag.Title = "Home";
  Layout = "~/Views/Shared/OneColLayer.cshtml";
}
@section HeaderContent {
  <h1>Left Content</h1>
}
<div>my view content</div>

the content gets placed in the oneCol template now the base template.

results…

<div id="Content">
   <h1>Left Content</h1>
</div>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x