【问题标题】:Autocomplete issue with ASP.NET MVC 5ASP.NET MVC 5 的自动完成问题
【发布时间】:2014-02-02 14:24:31
【问题描述】:

我创建了 ASP.NET MVC5 项目,并添加了一些链接来获得工作的自动完成 jQuery 插件。

_布局页面

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    <link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />
    <link href="~/Content/themes/base/jquery.ui.autocomplete.css" rel="stylesheet" />
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/jquery")
    <script src="~/Scripts/jquery-ui-1.10.3.js"></script>

    <script>
        $(document).ready(function () {

            var availableTags = [
                "ActionScript",
                "AppleScript",
                "Asp",
                "BASIC",
                "C",
                "C++",
                "Clojure",
                "COBOL",
                "ColdFusion",
                "Erlang",
                "Fortran",
                "Groovy",
                "Haskell",
                "Java",
                "JavaScript",
                "Lisp",
                "Perl",
                "PHP",
                "Python",
                "Ruby",
                "Scala",
                "Scheme"
            ];
            $("#autocomplete").autocomplete({
                source: availableTags
            });
        });}
    </script>
</head>

但是当我打开该页面时,我遇到了错误

伙计们,我真的不明白我错过了什么?

有什么线索可以解决这个错误吗?

附:这是我得到的JS列表。

【问题讨论】:

  • 您的代码乍一看还可以。使用 Fiddler 检查下载哪些脚本并确认下载顺序。将BundleTable.EnableOptimizations = false; 添加到RegisterBundles 的底部以确保没有任何内容被隐藏/压缩/组合。
  • 话虽如此,您的代码底部似乎有一个额外的尾随}

标签: jquery .net jquery-autocomplete jquery-ui-autocomplete asp.net-mvc-5


【解决方案1】:

除了你尾随的} 之外,它似乎工作。

http://jsfiddle.net/38uGv/

最好用 fiddler 检查浏览器下载,不要忘记在 App_Start\BundleConfig.cs 文件的底部添加 BundleTable.EnableOptimizations = false; RegisterBundles 方法。该设置将停止 JS 文件的任何合并/缩小。

【讨论】:

    【解决方案2】:

    我发现出了什么问题……该死!

    所以最终的代码应该是这样的

    <!DOCTYPE html>
    <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/modernizr")   
        @Styles.Render("~/Content/css")
    
        @*<script type='text/javascript' src='http://code.jquery.com/jquery-1.8.3.js'></script>*@
        @*<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>*@
        @*<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">*@
    
        <link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />
        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/jqueryui")
        @Scripts.Render("~/bundles/bootstrap")
        <script>
            $(document).ready(function(){
    
                var availableTags = [
                    "ActionScript",
                    "AppleScript",
                    "Asp",
                    "BASIC",
                    "C",
                    "C++",
                    "Clojure",
                    "COBOL",
                    "ColdFusion",
                    "Erlang",
                    "Fortran",
                    "Groovy",
                    "Haskell",
                    "Java",
                    "JavaScript",
                    "Lisp",
                    "Perl",
                    "PHP",
                    "Python",
                    "Ruby",
                    "Scala",
                    "Scheme"
                ];
                $("#autocomplete").autocomplete({
                    source: availableTags
                });
            });
    
        </script>
    
    </head>
    <body>
        <div class="navbar navbar-inverse navbar-fixed-top">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    @Html.ActionLink("Application name", "Index", "Home", null, new { @class = "navbar-brand" })
                </div>
                <div class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        <li>@Html.ActionLink("Home", "Index", "Home")</li>
                        <li>@Html.ActionLink("About", "About", "Home")</li>
                        <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                    </ul>
                    @Html.Partial("_LoginPartial")
                </div>
            </div>
        </div>
        <div class="container body-content">
            @RenderBody()
            <hr />
            <footer>
                <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
            </footer>
        </div>
    
        @RenderSection("scripts", required: false) 
    </body>
    </html>
    

    重要的是你已经搬家了

      @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/jqueryui")
        @Scripts.Render("~/bundles/bootstrap")
    

    BODY 标签到HEADER

    BundleConfig 应该是这样的

     public class BundleConfig
        {
            // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
            public static void RegisterBundles(BundleCollection bundles)
            {
                bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                            "~/Scripts/jquery-{version}.js"));
    
                bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                               "~/Scripts/jquery-ui-{version}.js"));
    
                bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                            "~/Scripts/jquery.validate*"));
    
                // 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"));
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      • 2023-03-16
      • 2016-10-26
      • 1970-01-01
      • 2011-01-24
      • 2011-04-15
      相关资源
      最近更新 更多