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

标签 asp.net-mvc asp.net-mvc-3 partial-views

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

相关文章:

ASP.NET 动态数据和成员资格(角色)

validation - MVC3 客户端验证不起作用

asp.net-mvc-3 - 如何使用 Asp.Net MVC 3 和 Razor 获得特定于 View 的 <head> 内容?

ruby-on-rails - Rails,将路径变量传递给部分

javascript - 如何指定 Kohana PHP Partial view CSS 和 JS 依赖

c# - Azure 应用服务 File.ReadAllText 返回文件未找到异常,但文件存在于该位置

asp.net-mvc - 将 LINQ to EF 投影到 View 模型时出现异常。获取 "only parameterless constructor and initializers"异常

asp.net - 最佳方法 : Mutliple content place holders in MVC

c# - 如何将部分 View 呈现为字符串?

asp.net-mvc - ASP.net MVC 3 项目文件未显示在新项目对话框中?