MVC Razor @section does not understand scripts

I have:

  • VS 2013
  • MVC 5.2.2
  • Razor 3.2.2

Let me know if you need to know anything else.

This problem occurs on only one page view.

I have a view with all HTML tags that were closed correctly

This is 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: "Unable to resolve sectional scripts"

When I run, I get an 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 problem.

Any ideas on how I can fix this?

+6
source share
2 answers

The exception message says:

The following sections are defined, but were not displayed for the layout page "~ / Views / Shared / _LayoutInquiry.cshtml": "Scripts".

This indicates that your _LayoutInquiry.cshtml page _LayoutInquiry.cshtml not set a placeholder for the Scripts section. You can do this by adding @RenderSection("Scripts", false) to your layout page in the appropriate place, where false indicates that views may not necessarily supply this section.

Now that your view is displayed, the contents of the @section Scripts { ... } in your view will be displayed at the location of the @RenderSection("Scripts", false) call on your layout page.

+17
source

For others who may be here. Everything works well, but ReSharper complains about the lack of a section and styles.

In our case, we had _Layout.cshtml and _Layout.Mobile.cshtml in the shared folder.

0
source

Source: https://habr.com/ru/post/1203898/


All Articles