'MVC Razor @section doesn't understand Scripts
I have:
- VS 2013
- MVC 5.2.2
- Razor 3.2.2
Let me know if there's anything else you need to know.
This problem only happens on one view page.
I have a View with all HTML tags closed off properly
It's your standard view...
@model MyNameSpace.Models.Inquiry
@{
var hasFormSetup = Model != null && Model.FormSetup != null && Model.FormSetup.Count > 0;
if (hasFormSetup)
{
ViewBag.Title = Model.FormSetup[0].InquiryValue;
}
Layout = "~/Views/Shared/_LayoutInquiry.cshtml";
}
<style scoped>
ul {
list-style-type: none;
}
</style>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="container" style="margin-top: 5px;">
etc...
</div>
<div class="container" >
etc...
</div>
}
@section Scripts
{
<script type="text/javascript">
$(document).ready(function () {
etc...
});
</script>
}
but...
on the line
@section Scripts
Resharper reports: "Cannot resolve section Scripts"
When I run, I get the exception:
Source: System.Web.WebPages
Target: Void VerifyRenderedBodyOrSections()
Type: HttpException
Message: The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_LayoutInquiry.cshtml": "Scripts".
Stack: at System.Web.WebPages.WebPageBase.VerifyRenderedBodyOrSections()
at System.Web.WebPages.WebPageBase.PopContext()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.WebPages.WebPageBase.<>c__DisplayClass3.<RenderPageCore>b__2(TextWriter writer)
at System.Web.WebPages.HelperResult.WriteTo(TextWriter writer)
at System.Web.WebPages.WebPageBase.Write(HelperResult result)
at System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body)
at System.Web.WebPages.WebPageBase.PopContext()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
etc...
No other page has this issue.
Any ideas as to how I can fix this?
Solution 1:[1]
The exception message says:
The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_LayoutInquiry.cshtml": "Scripts".
This suggests that your _LayoutInquiry.cshtml page does not setup a placeholder for a Scripts section. You can do this by adding @RenderSection("Scripts", false) to your layout page in the appropriate location, where the false indicates that views may optionally supply this section.
Now, when your view renders, the contents of @section Scripts { ... } in your view will be rendered into the location of the @RenderSection("Scripts", false) call in your layout page.
Solution 2:[2]
For others that may end up here. Everything is working OK but ReSharper complains about Section and Styles missing.
Our case was that we had _Layout.cshtml and _Layout.Mobile.cshtml in the shared folder.
Solution 3:[3]
I had this same issue but using Rider instead of VS. The code was actually legit, but some cache got in the way.
So the solution was to "Invalidate and Restart" - found in File > Invalidate caches...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Timothy Shields |
| Solution 2 | Mihail Shishkov |
| Solution 3 | KyleMit |
