asp.net-mvc - MVC Razor @section 不理解脚本

标签 asp.net-mvc razor

我有:

  • VS 2013
  • MVC 5.2.2
  • Razor 3.2.2

如果您还有其他需要了解的信息,请告诉我。

此问题仅发生在一个 View 页面上。

我有一个 View ,所有 HTML 标签都正确关闭

这是您的标准 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>
}

但是...

上线

@section Scripts

Resharper 报告:“无法解析部分脚本”

当我运行时,出现异常:

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

没有其他页面有此问题。

关于如何解决这个问题有什么想法吗?

最佳答案

异常消息显示:

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

这表明您的 _LayoutInquiry.cshtml 页面没有为 Scripts 部分设置占位符。您可以通过将 @RenderSection("Scripts", false) 添加到布局页面的适当位置来完成此操作,其中 false 表示 View 可以选择提供此部分。

现在,当您的 View 渲染时, View 中的 @section Scripts { ... } 的内容将被渲染到 @RenderSection("Scripts", false ) 在布局页面中调用。

关于asp.net-mvc - MVC Razor @section 不理解脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26168593/

相关文章:

sql - 错误消息 : (provider: Shared Memory Provider, 错误 : 0 - No process is on the other end of the pipe. )

c# - Entity Framework 中的共享连接字符串

js 文件中的 jQuery ajax

vb.net - VB.Net 中的 CheckBoxFor Nullable Boolean

asp.net-mvc - mvc4 区域内的路由操作无法映射操作

c# - ASP.NET MVC : populate (bind data) in listbox

c# - MVC5 Razor html.dropdownlistfor 当值在数组中时设置选择

c# - 将文本框值复制到另一个 - ASP.NET MVC Razor

c# - 如何在 visual studio 中访问 SQL 数据库? ( Razor cshtml,C#)

asp.net-mvc - Spark View 引擎与类 Html 属性的绑定(bind)