为什么Asp.Net MVC 5看跌@ Scripts.Render在_Layout.cshtml底部(QUOT;〜/捆绑/ jQuery和QUOT)? [英] Why Asp.Net MVC 5 put @Scripts.Render("~/bundles/jquery") at the bottom in _Layout.cshtml?

查看:173
本文介绍了为什么Asp.Net MVC 5看跌@ Scripts.Render在_Layout.cshtml底部(QUOT;〜/捆绑/ jQuery和QUOT)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把<脚本>其中一个(也是唯一一个)CSHTML文件的主体,它使用的模板,它们会导致错误,因为jQuery是不使用jQuery 块没有加载。

什么是放 @ Scripts.Render点(〜/包/ jQuery的)在_Layout.cshtml底部文件?

底部的 _Layout.cshtml

  @RenderBody()
    <小时/>
    <页脚GT&;
    < /页脚>
< / DIV>    @ Scripts.Render(〜/包/ jQuery的)
    @ Scripts.Render(〜/包/引导)
    @RenderSection(脚本,必需:false)
< /身体GT;
< / HTML>

下面显示了CSHTML文件生成的源。

 <脚本>
    $(文件)。就绪(函数(){/// $没有定义。
        // .....
    });
< / SCRIPT>    <小时/>
    <页脚GT&;
    < /页脚>
< / DIV><脚本的src =/脚本/ jQuery的-1.10.2.js>< / SCRIPT>
<脚本的src =/脚本/ bootstrap.js>< / SCRIPT>


解决方案

您可以使用部分:

在您的布局:

  ...
<脚本的src =/脚本/ jQuery的-1.10.2.js>< / SCRIPT>
<脚本的src =/脚本/ bootstrap.js>< / SCRIPT>
@RenderSection(脚本,必需:false)
...

在你CSHTML:

  @section脚本{
    <脚本>
        $(文件)。就绪(函数(){/// $没有定义。
            // .....
        });
    < / SCRIPT>
}

I put <script> blocks which use jQuery in the body of one (and only one) cshtml file which uses the template and they causes error because jQuery is not loaded yet.

What's the point to put the @Scripts.Render("~/bundles/jquery") at the bottom of _Layout.cshtml file?

The bottom of the _Layout.cshtml.

    @RenderBody()
    <hr />
    <footer>
    </footer>
</div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

The following shows the generated source of the cshtml file.

<script>
    $(document).ready(function () { /// $ not defined.
        // .....
    });
</script>

    <hr />
    <footer>
    </footer>
</div>

<script src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/bootstrap.js"></script>

解决方案

You can use sections :

in your layout :

...
<script src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/bootstrap.js"></script>
@RenderSection("scripts", required: false)
...

in yours cshtml :

@section scripts {
    <script>
        $(document).ready(function () { /// $ not defined.
            // .....
        });
    </script>
}

这篇关于为什么Asp.Net MVC 5看跌@ Scripts.Render在_Layout.cshtml底部(QUOT;〜/捆绑/ jQuery和QUOT)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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