asp.net-mvc MVC Razor @section 不理解脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26168593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC Razor @section doesn't understand Scripts
提问by Robert Achmann
I have:
我有:
- VS 2013
- MVC 5.2.2
- Razor 3.2.2
- VS 2013
- MVC 5.2.2
- 剃刀 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
我有一个视图,所有 HTML 标签都正确关闭
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"
Resharper 报告:“无法解析部分脚本”
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?
关于如何解决这个问题的任何想法?
回答by Timothy Shields
The exception message says:
异常消息说:
The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_LayoutInquiry.cshtml": "Scripts".
以下部分已定义但尚未为布局页面“~/Views/Shared/_LayoutInquiry.cshtml”呈现:“脚本”。
This suggests that your _LayoutInquiry.cshtmlpage does not setup a placeholder for a Scriptssection. You can do this by adding @RenderSection("Scripts", false)to your layout page in the appropriate location, where the falseindicates that views may optionally supply this section.
这表明您的_LayoutInquiry.cshtml页面没有为某个Scripts部分设置占位符。您可以通过@RenderSection("Scripts", false)在适当的位置添加到您的布局页面来做到这一点,其中false指示视图可以选择提供此部分。
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.
现在,当您的视图呈现时,您的视图中的内容@section Scripts { ... }将被呈现到@RenderSection("Scripts", false)布局页面中调用的位置。
回答by Mihail Shishkov
For others that may end up here. Everything is working OK but ReSharper complains about Section and Styles missing.
对于其他可能会在这里结束的人。一切正常,但 ReSharper 抱怨缺少部分和样式。
Our case was that we had _Layout.cshtml and _Layout.Mobile.cshtml in the shared folder.
我们的情况是我们在共享文件夹中有 _Layout.cshtml 和 _Layout.Mobile.cshtml。

