c# - 为什么 jquery 在布局中定义的页面上不起作用

标签 c# asp.net-mvc asp.net-mvc-4 razor

我有一个 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/

相关文章:

jQuery 这真的应该被视为硬编码值吗?

c# - ASP.NET 声音/音频录制?

c# - 动态创建 WrapPanel 按钮

asp.net-mvc - 如何在 Asp.Net MVC 中将数据表转换为 POCO 对象?

asp.net - ASP.NET 应用程序中什么消耗了超过 65% 的时间?

c# - 设置文本框格式以获取 MVC 上的 IP 地址

c# - 在 asp.net mvc View 中将字符串显示为 html

c# - 在方法签名中使用异步时,任务取消异常不会冒泡

c# - 如何使用类属性使用扩展方法

c# - 如何在 C#/.NET 中找到本地计算机的 FQDN?