asp.net-mvc Razor MVC 渲染部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27290698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Razor MVC render section
提问by Miomir Dancevic
I am using master layout of Razor MVC that have something like this
我正在使用 Razor MVC 的主布局,它有这样的东西
@RenderSection("scripts", required: false)
And have partival view
并有部分观点
_partial.cshtml
_partial.cshtml
That have this
有这个
@section scripts
{
@Scripts.Render("~/bundles/jquery")
}
And another partial in partial
另一个部分在部分
_partial_inside_partial.cshtml
_partial_inside_partial.cshtml
That also have
那也有
@section scripts
{
<script>
$('div').addClass('red');
</script>
}
The problem i have this code inside partial, its load at he middle of the page, and jquery is at the bottom?
问题是我在partial里面有这段代码,它在页面中间加载,而jquery在底部?
采纳答案by Ashley Lee
Sections don't work the same way in partial views. In order to achieve what you're after, your going to have to move your scripts to a file, include an HTML helper, then call that HTML helper to render your scripts for each partial view that is loaded.
部分在部分视图中的工作方式不同。为了实现您所追求的目标,您必须将脚本移动到一个文件中,包含一个 HTML 帮助程序,然后调用该 HTML 帮助程序为每个加载的局部视图呈现您的脚本。
Use this as a reference: Using sections in Editor/Display templates
将此用作参考:在编辑器/显示模板中使用部分

