RenderSection在ASP.NET MVC3中的部分视图中不起作用

Cic*_*ami 10 asp.net-mvc partial-views asp.net-mvc-3

在我的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>
Run Code Online (Sandbox Code Playgroud)

然后在我的Index.cshtml视图中我有:

@model MyApp.ViewModels.MyViewModel
@{ Html.RenderPartial("MyPartial", Model);  }
Run Code Online (Sandbox Code Playgroud)

如果我将它@section ScriptContent放在Index.cshtml中,它会正确显示.如果我将它放在我的部分视图中MyPartial.cshtml:

@model MyApp.ViewModels.MyViewModel

@section ScriptContent {
     <script src="@Url.Content("~/Scripts/Filters.js")" type="text/javascript"></script>    
} 
Run Code Online (Sandbox Code Playgroud)

在我的页面来源中,我有:

</body>
     <script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>    
</html>
Run Code Online (Sandbox Code Playgroud)

意思@section是没有执行.可能是什么原因?谢谢

Ror*_*san 13

无法@section从局部视图中设置布局.作为一种解决方法,您可以调用一个呈现必要<script>和HTML 的动作- 虽然这不是很优雅.

  • 坦斯克!因此,除了您建议的方案之外,唯一可能的解决方案是在我的视图中移动@section (2认同)