I am using ASP.NET MVC 3 with the Razor view engine.
I have a layout view called _Root.cshtml. The HTML markup looks like this:
<body>
<div id="hd"></div>
@RenderBody()
<div id="ft"></div>
@RenderSection("JavaScriptBodySection", false)
</body>
The @RenderBody() is there because the layout differs depending on what view you are at. The JavaScriptBodySection is where I want all my JavaScript to go that is used by a view (injected by the view).
I have another layout that uses this _Root layout called _Lr.cshtml. The HTML markup looks like this:
<div id="bd">
<!-- Layout code here -->
@RenderBody()
</div>
Then finally my Index view uses this _Lr layout and the markup looks like this:
@{
Layout = "~/Views/Shared/_Lr.cshtml";
}
<div class="main-content">
<!-- Some stuff here -->
</div>
@section JavaScriptBodySection
{
<script type="text/javascript"></script>
}
It is complaining that there is no JavaScriptBodySection in _Lr. I don’t want it here because then my JavaScript is beneath my div with id bd, I want it just before the closing body tag (in _Root). How do I do this?
In
_Lr.cshtmlyou need to redefine the section:This obviously assumes that
_Lr.cshtmlderives fromRoot.cshtml(nested layouts).