我正在使用 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/