Asp.net mvc 3 MVC 3-嵌套布局-截面不';不可在区域内渲染 问题:
给定此嵌套布局结构: ~/Views/Shared/_layoutBase.cshtml ~/Views/Shared/_layout.cshtmlAsp.net mvc 3 MVC 3-嵌套布局-截面不';不可在区域内渲染 问题:,asp.net-mvc-3,razor,Asp.net Mvc 3,Razor,给定此嵌套布局结构: ~/Views/Shared/_layoutBase.cshtml ~/Views/Shared/_layout.cshtml \u layout.cshtml: @section footerScripts{ @RenderSection("footerScripts", false) } @{ Layout = "~/Views/Shared/_Layout.cshtml"; } <!DOCTYPE h
\u layout.cshtml
:
@section footerScripts{
@RenderSection("footerScripts", false)
}
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<!DOCTYPE html>
<html>
<body>
@RenderBody()
<script type="text/javascript">
@RenderSection("footerScripts", false)
</script>
</body>
</html>
“内容”视图:
节footerScripts
的内容永远不会在区域中的视图中呈现。它确实会在~/Views
文件夹下的视图中渲染
区域\u ViewStart.cshtml
:
@section footerScripts{
@RenderSection("footerScripts", false)
}
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<!DOCTYPE html>
<html>
<body>
@RenderBody()
<script type="text/javascript">
@RenderSection("footerScripts", false)
</script>
</body>
</html>
问题:
你能看出什么不对劲吗 我无法重现这个问题。这是我的设置和步骤
~/Views/Shared/\u LayoutBase.cshtml
:
@section footerScripts{
@RenderSection("footerScripts", false)
}
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<!DOCTYPE html>
<html>
<body>
@RenderBody()
<script type="text/javascript">
@RenderSection("footerScripts", false)
</script>
</body>
</html>
~/Areas/admin/Views/Test/Index.cshtml
视图:
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
@section footerScripts{
alert('ok');
}
@{
ViewBag.Title=“Index”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
指数
@节页脚脚本{
警报(“正常”);
}
/admin/test/index
请给达林投票,但这才是真正的答案。+1:我相信你。我明天也要把它全部做完。我的代码中肯定有错误,但如果我能看到它,我就大发雷霆。非常感谢。这正是我想要的。布局中的嵌套渲染。。。谢谢!除此之外,如果您想添加一个区域,例如“admin”,您可以将其添加到区域的{Layout=“~/Views/Shared/_AdminLayout.cshtml”;}的_ViewStart.cshtml中,从根共享文件夹添加一个类似于此的_AdminLayout.cshtml@{Layout=“~/Views/Shared/_LayoutBase.cshtml”}@section-footerscript{@RenderSection(“footerscript”,false)}@RenderBody()如果这解决了您的问题,您应该接受自己的答案。