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

时间:2022-10-16 04:10:02

In my ASP.NET MVC3 project I have a standard _Layout.cshtml generated by Visual Studio 2010 and after closing my <body> tag, I place a RenderSection:

在我的ASP.NET MVC3项目中,我有一个由Visual Studio 2010生成的标准_Layout.cshtml,在关闭我的标签之后,我放置了一个RenderSection:

_Layout.cshtml:

_Layout.cshtml:

</body>
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
@RenderSection("ScriptContent", required: false)
</html>

Then in my Index.cshtml View I have:

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

@model MyApp.ViewModels.MyViewModel
@{ Html.RenderPartial("MyPartial", Model);  }

If I place the @section ScriptContent in the Index.cshtml it displays correctly. If I place it in my Partial View MyPartial.cshtml:

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

@model MyApp.ViewModels.MyViewModel

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

In my page source I have:

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

</body>
     <script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>    
</html>

Meaning the @section is not executed. What might be the cause? Thanks

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

1 个解决方案

#1


11  

It is not possible to set an @section in the layout from a partial view. As a workaround you could instead call an action which renders the necessary <script> and HTML - although this is not very elegant.

无法在局部视图中在布局中设置@section。作为一种解决方法,您可以调用一个呈现必要的

#1


11  

It is not possible to set an @section in the layout from a partial view. As a workaround you could instead call an action which renders the necessary <script> and HTML - although this is not very elegant.

无法在局部视图中在布局中设置@section。作为一种解决方法,您可以调用一个呈现必要的