mvc 4底部的脚本-不执行& lt; scripts& gt;在“查看"页面中 [英] scripts at the bottom in mvc 4 - Does not execute <scripts> in View page

查看:72
本文介绍了mvc 4底部的脚本-不执行& lt; scripts& gt;在“查看"页面中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MVC4 中,视图的页面底部包含 script 元素.

In MVC4, Views have script elements included at the bottom of the page.

例如.在 _layout.cshtml (在 Shared 文件夹下)中,< script> 标记位于最底部.虽然我知道这可以使页面加载更快,但问题实际上是这样的:

Eg. In _layout.cshtml (under Shared folder), <script> tags are placed at the very bottom. While I understand that this makes the page load faster, the problem is actually this:

在我的 Views 中,有时我碰巧使用某些脚本,例如 autocomplete ,如下所示:

In my Views, sometimes I happen to use some scripts like autocomplete, something like below:

< script type ="text \ javascript">......</script>

不起作用,因为 scripts 标记包含在页面的最后.(当时甚至没有加载jQuery)

which is not functional because scripts tag are included at the very end of the page. (JQuery is not even loaded at that time)

痛苦的是,我不得不将 Script Bundle渲染移到页面顶部.喜欢,

Painfully, I had to move the Script Bundle rendering to the top of the page. Like,

render jquery first
then load scripts

那么,我是否缺少某些内容,还是应该始终将 scripts 标记保留在 _layout.cshtml 页面的顶部?

so, am I missing something or I should always keep my scripts tag at the top of the _layout.cshtml page ?

感谢您的回复.

推荐答案

好.关键是:

将脚本放置在页面的任意位置,但将其包含在 @section脚本中.

put your scripts anywhere in your page but enclose them within a @section Scripts.

这将确保无论您将脚本放置在哪里, _layout.cshtml'都将使用随附的 JQuery`来呈现它们.

This will ensure that wherever you place your scripts, the _layout.cshtml' renders them with theJQuery` included.

因为,在您的 _layout.cshtml 中,您有以下内容:

Because, in your _layout.cshtml you have this:

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

这篇关于mvc 4底部的脚本-不执行&amp; lt; scripts&amp; gt;在“查看"页面中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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