使用ajax和jQuery在asp.net mvc4中自动完成文本框

时间:2022-03-08 05:45:49

I am trying to fetch company name in textbox as autocomplete. 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将调用success函数,并且还会正确获取记录,但文本框中没有自动完成建议。

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>

1 个解决方案

#1


-1  

Please try removing

请尝试删除

~/script/jquery-2.0.3.js

from the script references in your application, and that should work for you....

从您的应用程序中的脚本引用,这应该适合您....

#1


-1  

Please try removing

请尝试删除

~/script/jquery-2.0.3.js

from the script references in your application, and that should work for you....

从您的应用程序中的脚本引用,这应该适合您....