简体   繁体   English

为什么我的select2自动完成方法没有执行?

[英]Why isn't my select2 autocomplete method getting executed?

I'm trying to implement the select2 on my master layout of my ASP.NET MVC 4 website. 我正在尝试在ASP.NET MVC 4网站的主布局上实现select2

I want it to, as soon as the user starts typing (minimum of 2 letters), call my method to query the database using the letters the user has already typed. 我希望它在用户开始键入时(至少2个字母),调用我的方法以使用用户已经键入的字母来查询数据库。 Except, when I start typing, my method never gets called. 除了,当我开始键入时,我的方法永远不会被调用。 I threw in some alerts and I'm able to see what I'm typing, but the select2 isn't firing, I think. 我发出了一些警报,我能够看到我输入的内容,但我认为select2没有触发。

Here's the script and css references in the tag: 这是标记中的脚本和CSS参考:

<script src="/Content/select2/select2.js"></script>
<link href="/Content/select2/select2.css" rel="stylesheet" />

Here's the search box in my layout.cshtml file: 这是我的layout.cshtml文件中的搜索框:

<div class="navbar-header hidden-xs pull-right" style="padding-top: 10px; margin-left: 10px;">
  <input id="search" class="form-control" placeholder="Search..." type="text" data-autocomplete-url="/Home/AutoFillValues" />
</div>

And here's the bottom of my layout page where the select2 stuff appears: 这是我的布局页面的底部,其中出现了select2内容:

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

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

<script type="text/javascript">
    $(document).ready(function () {
        $("#search").select2({
            placeholder: "Search...",
            minimumInputLength: 2,
            ajax: {
                url: "~/Home/AutoFillSearch",
                dataType: 'jsonp',
                data: function(search) {
                    return {
                        q: search
                    };
                },
                results: function(search, page) {
                    return { results: data.Results };
                }
            }
        });
    });
</script>

Lastly, here's my controller method (I'm not worried about the code here yet, I just want to see my breakpoint get hit at the first line...): 最后,这是我的控制器方法(我现在还不担心这里的代码,我只想看看我的断点在第一行被击中...):

public ActionResult AutoFillValues()
{
    // Get all wrestlers and all teams
    var players = _playerRepo.Query().Where(w => w.Division == "Division I" && w.Conference != null);
    var schools = players.Select(w => w.School).Distinct();

    ArrayList list = new ArrayList();

    foreach (var school in schools)
    {
        var hyperlinkFormat = "<a href=\"{0}\">{1}</a>";

        //@HttpUtility.UrlEncode(Model.Team.Name, System.Text.Encoding.UTF8)
        list.Add(string.Format(hyperlinkFormat, string.Format(RosterPath, school), string.Format(RosterText, school)));
    }

    foreach (var player in playerss)
    {
        var hyperlinkFormat = "<a href=\"{0}\">{1}</a>";

        //@HttpUtility.UrlEncode(Model.Team.Name, System.Text.Encoding.UTF8)
        list.Add(string.Format(hyperlinkFormat, string.Format(RosterPath, player.School), string.Format(RosterText, player.School)));
    }

    return Json(list.ToArray());
}

I had to upgrade to jQuery 2.1 (because of the issue with version 1.9 and the .live/.on functions) in order for my select2 to start firing. 我必须升级到jQuery 2.1(由于1.9版和.live / .on函数的问题),以便我的select2开始触发。

Now I just need to get my select2 styled... 现在,我只需要设置我的select2样式...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM