jQuery的脚本订购ASPNET MVC 4应用程序 [英] Jquery scripts order in ASPNET MVC 4 application

查看:157
本文介绍了jQuery的脚本订购ASPNET MVC 4应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个ASPNET MVC 4项目,jqGrid的。

I'm working on an ASPNET MVC 4 project with jqgrid.

有,ASPNET MVC 4需要将默认

There, ASPNET MVC 4 puts by default

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

在它的结束_Layout.cshtml文件。

in _Layout.cshtml file at the end of it.

现在,我有一个使用的jqGrid像

Now, I have a Index.cshtml which uses jqgrid like

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

所以我必须包括的jqGrid脚本像

So I must include jqgrid scripts like

@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>
}

但是,使用任何与.jqgrid之前,我需要的jqGrid脚本加载而这又需要jQuery的脚本加载,因此,jQuery的脚本需要在顶部,而不是在对_Layout.cshtml文件末尾。

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.

根据最佳实践的jQuery脚本需要在文件的结尾被加载,但如果我这样做,在Index.cshtml文件,它不知道jQuery是什么。

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.

我不能把jqquery脚本和下面的jqGrid脚本在_Layout.cshtml文件的底部,因为上面是使用的jqGrid脚本Index.cshtml文件内容。

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.

有我丢失的东西,以便能够把jQuery的在然后结束,仍然能够使用jQuery的东西的看法?

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?

谢谢!吉列尔莫。

推荐答案

__ 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>
}

但是,最好的做法是有一个单独的JavaScript文件与code,是这样的:

But the best practice would be to have a separate javascript file with your code, like this:

__ 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({ ... });

});

这篇关于jQuery的脚本订购ASPNET MVC 4应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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