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

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

问题描述

MVC4 中,视图在页面底部包含脚本元素。

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:

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

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

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

由于脚本标签包含在页面的最后。 (当时甚至没有加载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)

很遗憾,我不得不移动脚本包渲染 code>到页面顶部。像这样,

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

render jquery first
then load scripts

所以,我遗漏了什么东西还是应该始终将脚本标记保留在<$ c顶部$ c> _layout.cshtml 页面?

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

感谢您的回复。

推荐答案

好。关键是:

将脚本放到页面的任意位置,但将其放在内@section脚本

这将确保无论您将脚本放置在什么地方, _layout.cshtml'都将使用

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

因为,在您的 _layout.cshtml 中, :

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

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

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