RenderSection在ASP.NET MVC3的部分视图中不起作用 [英] RenderSection not working inside Partial View in ASP.NET MVC3

查看:225
本文介绍了RenderSection在ASP.NET MVC3的部分视图中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

_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>

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

Then in my Index.cshtml View I have:

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

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

If I place the @section ScriptContent in the Index.cshtml it displays correctly. If I place it in my Partial View 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>

意味着不执行@section.可能是什么原因?谢谢

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

推荐答案

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

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.

这篇关于RenderSection在ASP.NET MVC3的部分视图中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆