在我的 ASP.NET MVC3 项目中,我有一个标准 _Layout.cshtml
由 Visual Studio 2010 生成并在关闭我的 <body>
后标签,我放置一个 RenderSection
:
_Layout.cshtml:
</body>
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
@RenderSection("ScriptContent", required: false)
</html>
然后在我的 Index.cshtml
查看我有:
@model MyApp.ViewModels.MyViewModel
@{ Html.RenderPartial("MyPartial", Model); }
如果我放置 @section ScriptContent
在 Index.cshtml 中它显示正确。如果我将它放在我的部分 View 中 MyPartial.cshtml
:
@model MyApp.ViewModels.MyViewModel
@section ScriptContent {
<script src="@Url.Content("~/Scripts/Filters.js")" type="text/javascript"></script>
}
在我的页面源代码中,我有:
</body>
<script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
</html>
意思是@section
没有被执行。可能是什么原因?谢谢
最佳答案
无法设置 @section
从局部 View 的布局中。作为解决方法,您可以调用一个操作来呈现必要的 <script>
和 HTML - 尽管这不是很优雅。
关于asp.net-mvc - RenderSection 无法在 ASP.NET MVC3 中的部分 View 内工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13953258/