为什么 Asp.Net MVC 5 将 @Scripts.Render("~/bundles/jquery") 放在 _Layout.cshtml 的底部? [英] Why Asp.Net MVC 5 put @Scripts.Render("~/bundles/jquery") at the bottom in _Layout.cshtml?

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

问题描述

我将使用 jQuery 的 <script> 块放在一个(并且只有一个)使用模板的 cshtml 文件的主体中,它们会导致错误,因为 jQuery 尚未加载.

@Scripts.Render("~/bundles/jquery") 放在 _Layout.cshtml 文件底部有什么意义?

底部_Layout.cshtml.

 @RenderBody()<小时/><页脚></页脚>

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

下面是生成的cshtml文件的源码.

<小时/><页脚></页脚>

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

解决方案

您可以使用部分 :

在您的布局中:

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

在你的 cshtml 中:

@section 脚本 {<脚本>$(document).ready(function () {///$ 未定义.//.....});}

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(&quot;~/bundles/jquery&quot;) 放在 _Layout.cshtml 的底部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆