简体   繁体   English

使用Ajax和jQuery的ASP.NET MVC4中的自动完成文本框

[英]Autocomplete textbox in asp.net mvc4 using ajax and jQuery

I am trying to fetch company name in textbox as autocomplete. 我正在尝试在textbox获取公司名称作为自动完成功能。 When I run my project, Ajax will call the success function, and the record is also fetched correctly, but there are no autocomplete suggestions in the textbox . 当我运行项目时,Ajax将调用成功函数,并且记录也可以正确提取,但是在textbox没有autocomplete建议。

My view is: 我的看法是:

    $("#idcompanyname").autocomplete({
        source: function (request, response) {

            var customer = new Array();
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: '@Url.Action("Companymap", "admin")',
                data: "{'term':'" + document.getElementById('idcompanyname').value + "'}",
                dataType: "json",
                success: function (data) {
                    alert(data)

                    response($.map(data, function (v, i) {
                        var text = v.vcCompanyName;
                        alet(text)
                        if (text && (!request.term || matcher.test(text))) {
                            return {
                                label: v.vcCompanyName,
                                value: v.kCompanyId
                            };
                        }
                    }));
                },
                error: function(result) {
                    alert("No Match");
                }
            });
        }
    });
}

Here is Method on controller: 这是控制器上的方法:

       var query = db.tbaccounts.Where(t => t.vcCompanyName.ToLower()
                                           .StartsWith(term)).ToList();
       List<string> lst = new List<string>();
       foreach (var item in query)
       {
           lst.Add(item.vcCompanyName);
       }
       return Json(lst, JsonRequestBehavior.AllowGet);

Here is the referred Javascript: 这是引用的Javascript:

<script src="~/script/jquery-2.0.3.js"></script>
<script src="~/script/jquery-ui.js"></script>
<script src="~/js/jquery-1.10.2.js"></script>
<script src="~/js/jquery-ui.js"></script>

Please try removing 请尝试删除

~/script/jquery-2.0.3.js

from the script references in your application, and that should work for you.... 从您的应用程序中的脚本引用,这应该适合您...。

暂无
暂无

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

相关问题 我如何创建文本框,可以使用Asp.net mvc4中的jquery ajax在数据库中保存任何输入 - how can i create textbox which can save any input in database using jquery ajax in Asp.net mvc4 如何使用ASP.NET MVC,JavaScript,缓存,Ajax实现自动完成文本框 - How do I implement autocomplete textbox using asp.net mvc, javascript,cache, ajax 使用ASP.Net中的AJAX PageMethods从数据库中获取JQuery AutoComplete TextBox在Internet Explorer中不起作用 - JQuery AutoComplete TextBox from database using AJAX PageMethods in ASP.Net is not Working in Internet Explorer 如何使用jquery在asp.net中为文本框自动完成 - How to do autocomplete for textbox in asp.net using jquery jQuery自动完成建议未显示在文本框下方,也不显示在ASP.NET MVC页面底部 - Jquery autocomplete suggestion is not showing under textbox and showing at the bottom of the page in ASP.NET MVC jQuery自动完成-TypeError:自动完成不是ASP.NET MVC 5中的函数 - jQuery autocomplete - TypeError: autocomplete is not a function in asp.net mvc 5 jQuery日期选择器在ASP.Net MVC4中无法正常工作 - jQuery date picker is not working properly in ASP.Net MVC4 ASP.NET MVC4和JQuery-Twitter Feed脚本不起作用 - ASP.NET MVC4 & JQuery - Twitter Feed Script not working 使用jQuery Ajax的ASP.Net MVC 5无法发送__RequestVerificationToken - ASP.Net MVC 5 using jQuery Ajax unable to send __RequestVerificationToken 使用 JQuery Ajax 和 ASP.Net Mvc 的正确模式是什么? - What is the right pattern for using JQuery Ajax and ASP.Net Mvc?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM