不知道在哪里放置Scripts.Render for JQuery [英] Not sure where to place Scripts.Render for JQuery

查看:184
本文介绍了不知道在哪里放置Scripts.Render for JQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试调用JQuery中的自动完成功能时收到" 未捕获的TypeError:未定义不是函数 ",并且我假设我有自己的脚本.在错误的地方渲染. JQuery脚本应放在layout.cshtml中的什么位置?我的假设是最重要的,但是无论我将它们放在哪里,我似乎都无法使其正常工作.

I'm receiving a 'Uncaught TypeError: undefined is not a function' while trying to call the autocomplete function in JQuery and I'm assuming I have my Scripts.Render in the wrong place. Where shouldthe JQuery scripts go within the layout.cshtml? My assumption was at the top but I can't seem to get it working no matter where I put them.

这是我的布局文件的顶部:

Here is the top of my layout file:

<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryui")
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @Styles.Render("~/Content/themes/base/css")
</head>...

在底部,我有这个:

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

在查看"文件的底部,我使用了javascript:

At the bottom of my View file I have the javascript:

@section scripts{
    <script>
    $(document).ready(function () {

        $('#mpvalue').autocomplete({
            source: '@Url.Action("MyMethod")'
        });

    });
    </script>
}

在BundleConfig中,我也进行了一些捆绑操作:

I also have some bundling going on in the BundleConfig:

    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                "~/Scripts/jquery-{version}.js"));

    bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                "~/Scripts/jquery.validate*"));

    bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                  "~/Content/themes/base/jquery.ui.core.css",
                  "~/Content/themes/base/jquery.ui.resizable.css",
                  "~/Content/themes/base/jquery.ui.selectable.css",
                  "~/Content/themes/base/jquery.ui.accordion.css",
                  "~/Content/themes/base/jquery.ui.autocomplete.css",
                  "~/Content/themes/base/jquery.ui.button.css",
                  "~/Content/themes/base/jquery.ui.dialog.css",
                  "~/Content/themes/base/jquery.ui.slider.css",
                  "~/Content/themes/base/jquery.ui.tabs.css",
                  "~/Content/themes/base/jquery.ui.datepicker.css",
                  "~/Content/themes/base/jquery.ui.progressbar.css",
                  "~/Content/themes/base/jquery.ui.theme.css"));
    // Use the development version of Modernizr to develop with and learn from. Then, when you're
    // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
    bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                "~/Scripts/modernizr-*"));

    bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
              "~/Scripts/bootstrap.js",
              "~/Scripts/respond.js"));

    bundles.Add(new StyleBundle("~/Content/css").Include(
              "~/Content/bootstrap.css",
              "~/Content/site.css"));

从我在网络上其他帖子和示例中所阅读的内容来看,所有内容似乎都放在正确的位置.

From what I've read on other posts and examples on the web, everything appears to be in the correct places.

我该怎么办才能引发" 未捕获的TypeError:未定义不是函数 "?

What am I doing to throw the 'Uncaught TypeError: undefined is not a function'?

任何帮助将不胜感激

谢谢!

这是在ASP.NET MVC 5上运行的

This is running on ASP.NET MVC 5

推荐答案

包含的顺序实际上与JS错误没有任何关系.看起来您的任何脚本包中实际上都不包含jQueryUI JavaScript文件.您有一堆用于jQueryUI的CSS文件,但没有JS文件.在问题中包含的代码中,您尝试呈现一个名为"jqueryui"的捆绑包,但BundleConfig中没有使用该名称定义的捆绑包.

The order of your includes doesn't actually have anything to do with your JS error. It doesn't look like the jQueryUI JavaScript file is actually included in any of your script bundles. You have a bunch of CSS files for jQueryUI, but no JS files. In the code that you included in the question, you're trying to render a bundle called "jqueryui" but there is not a bundle defined with that name in the BundleConfig.

假设您已将jQueryUI添加到项目中,则jQueryUI脚本包应如下所示(取自MVC4模板):

Assuming you have added jQueryUI to the project, the jQueryUI script bundle should look like this (taken from the MVC4 template):

bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
            "~/Scripts/jquery-ui-{version}.js"));

这篇关于不知道在哪里放置Scripts.Render for JQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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