我有一个 razor
布局,它加载了 css jquery 等
当我添加 Controller 操作 View 时,我将布局设置为 _Layout.cshtml
但是 jquery 在该页面中不起作用。
所以我必须在每个 View 中添加 @Scripts.Render("~/bundles/jquery")
。
为什么导入的jquery不是继承自_Layout.cshtml
?正常吗?
最佳答案
这应该在 _Layout.cshtml 的底部附近:
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
这应该在您的 subview 中:
@section scripts {
@Scripts.Render("~/scripts/jquery.infinitescroll.js") // Or some other script
}
subview 中的所有脚本(引用 jquery)都需要放在脚本部分,而不是放在页面的主体中。这包括任何想要使用 jquery 的脚本标签。例如,我的一个页面有以下内容:
@section scripts {
@Scripts.Render("https://maps.googleapis.com/maps/api/js?key=&sensor=false")
<script src="~/scripts/google-maps-3-vs-1-0.js"></script>
<script>
Stuff here
</script>
}
关于c# - 为什么 jquery 在布局中定义的页面上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18860508/