c# - MVC @RenderSection "sections have been defined but have not been rendered"脚本。多级页面时

标签 c# asp.net-mvc razor asp.net-mvc-4

我正在使用 MVC v4。
我有一个“_BootstrapLayout”页面,它定义了所有 twitter bootstrap 等内容,一个定义网站布局、导航栏等的主页,以及从主页继承的网站页面。

_BootstrapLayout.cshtml

_MainPage.cshtml @{ Layout = "~/Views/Shared/_BootstrapLayout.cshtml"; }

Index.cshtml @{Layout = "~/Views/Shared/_MainPage.cshtml";}

所以 母版页 --> 主页 --> 站点页面

_BootstrapLayout 页面包含脚本的呈现部分

@RenderSection("scripts", required: false)

我想将脚本部分添加到 Index.cshtml 页面,但是当我这样做时出现异常

@section scripts {
    <script type="text/javascript">

        $(document).ready(function () {
...

        });

    </script>
}

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

所以我在_MainPage.cshtml 中添加了一个空的@section 脚本,仍然是同样的问题? 即使我将代码添加到 _MainPage 脚本部分,我仍然会遇到同样的错误。无论我将 @section 放在 _MainPage 中的什么位置,仍然会出现相同的错误。

如果我故意不关闭该部分(即删除 }),那么我会收到一个错误,指示该部分不正确,因此它正在解析 _MainPage 中的部分。

在这种情况下,如何让网站页面上的脚本的@RenderSections 正常工作?

最佳答案

您需要在 _MainPage.cshtml 中重新定义该部分。

_BootstrapLayout.cshtml

@RenderSection("scripts", false)

_MainPage.cshtml

@section scripts
{
   @RenderSection("scripts", false)
}

索引.cshtml

@section scripts
{
   <script>
     // etc.
   </script>
}

关于c# - MVC @RenderSection "sections have been defined but have not been rendered"脚本。多级页面时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14479442/

相关文章:

c# - 使 ASP.NET Identity 2.0 电子邮件确认 token 适用于 WCF 和 MVC

asp.net-mvc - 使用 ASP.NET MVC3 在自定义 Htmlhelper 中查找区域名称和 Controller 名称

c# - Log4net SmtpAppender 不工作

c# - WinRT Flyout 中的数据绑定(bind)未更新

C# 何时设置具有默认值的实例变量?

c# - 在 ASP.Net MVC 中保持干燥

asp.net - 为什么 VS 2013 将 ASP.NET MVC 4 web.config 复制到 bin 并重命名?

c# - C# 中的简单 Lua 语法检查

visual-studio-2010 - Razor 编辑器崩溃 VS2010

javascript - MVC 4 分部 View 是否使用其父 View 的 .css 和 .js,或者是否必须将 .css 和 .js 添加到分部 View 中?