简体   繁体   English

在ASPNET MVC 4应用程序中命令Jquery脚本

[英]Jquery scripts order in ASPNET MVC 4 application

I'm working on an ASPNET MVC 4 project with jqgrid. 我正在使用jqgrid开发ASPNET MVC 4项目。

There, ASPNET MVC 4 puts by default 在那里,ASPNET MVC 4默认放置

@Scripts.Render("~/bundles/jquery")

in _Layout.cshtml file at the end of it. 在_Layout.cshtml文件的末尾。

Now, I have a Index.cshtml which uses jqgrid like 现在,我有一个使用jqgrid的Index.cshtml

<script type="text/javascript">
    jQuery("#ajaxGrid").jqGrid({

So I must include jqgrid scripts like 所以我必须包括jqgrid脚本

@section jqgridScripts
{
    <script src="@Url.Content("~/Scripts/jqgrid/i18n/grid.locale-en.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jqgrid/js/jquery.jqGrid.min.js")" type="text/javascript"></script>
}

But before using anything with .jqgrid I need jqgrid scripts loaded which in turn needs jquery scripts loaded, thus, jquery scripts needs to be at the top instead of at the end on _Layout.cshtml file. 但在使用.jqgrid之前,我需要加载jqgrid脚本,而这需要加载jquery脚本,因此,jquery脚本需要位于_Layout.cshtml文件的顶部而不是末尾。

According to best practices jquery scripts needs to be loaded at the end of the file, but if I do that, in Index.cshtml file it doesn't know what jQuery is. 根据最佳实践,需要在文件末尾加载jquery脚本,但是如果我这样做,在Index.cshtml文件中它不知道jQuery是什么。

I can't put jqquery scripts and below jqgrid scripts at the bottom of _Layout.cshtml file since above that is the Index.cshtml file content which uses jqgrid scripts. 我不能把jqquery脚本和jqgrid脚本放在_Layout.cshtml文件的底部,因为上面是使用jqgrid脚本的Index.cshtml文件内容。

Is there something I'm missing in order to be able to put jQuery at then end and still be able to use something with jquery in the view? 是否有一些我缺少的东西,以便能够在结束时放置jQuery并仍然能够在视图中使用jquery的东西?

Thanks! 谢谢! Guillermo. 吉列尔莫。

__Layout.cshtml: __Layout.cshtml:

@Scripts.Render("~/bundles/jquery")
@RenderSection("jqgridScripts", required: false);

@* for script in current page *@
@RenderSection("pageScripts", required: false);

Index.cshtml: Index.cshtml:

@section pageScripts
{
<script type="text/javascript">
    jQuery("#ajaxGrid").jqGrid({
    ... 
</script>
}

But the best practice would be to have a separate javascript file with your code, like this: 但最好的做法是在代码中使用单独的javascript文件,如下所示:

__Layout.cshtml: __Layout.cshtml:

@Scripts.Render("~/bundles/jquery")
@RenderSection("pageScripts", required: false);

Index.cshtml: Index.cshtml:

@section pageScripts
{
    <script src="@Url.Content("~/Scripts/jqgrid/i18n/grid.locale-en.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jqgrid/js/jquery.jqGrid.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/Site/myscript.js")" type="text/javascript"></script>
}

myscript.js: myscript.js:

$(function() {

   jQuery("#ajaxGrid").jqGrid({ ... });

});

I have encountered this issue as well. 我也遇到过这个问题。 It seems kind of counter-intuitive to not bundle jQuery itself, but it's what must be done. 不捆绑jQuery本身似乎有点违反直觉,但这是必须要做的。 Throw a jQuery script reference up in the header. 在标题中抛出一个jQuery脚本参考。 If you set your jQuery script src attribute to that of the minified google-hosted instance, there is a high likelihood that the script is already cached in your client anyhow. 如果您将jQuery脚本src属性设置为缩小的google托管实例的属性,则很可能无论如何脚本已经缓存在您的客户端中。

Here is a nice reference... 这是一个很好的参考......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM