gpt4 book ai didi

asp.net-mvc - RenderSection 无法在 ASP.NET MVC3 中的部分 View 内工作

转载 作者:行者123 更新时间:2023-12-02 05:32:12 28 4
gpt4 key购买 nike

在我的 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com