使用 ASP MVC 的 jQuery UI 自动完成

发布于 2024-09-24 12:53:36 字数 2228 浏览 5 评论 0原文

我正在尝试让 jQuery Automcomplete 工作,但它不会按照我想要的方式工作:P 这是我的代码:

JavaScript:

        $("#CustomerID").autocomplete({
            source: function(request, response) {
                $.ajax({
                    type: "POST",
                    url: "/customer/search",
                    dataType: "json",
                    data: {
                        term: request.term
                    },
                    error: function(xhr, textStatus, errorThrown) {
                        alert('Error: ' + xhr.responseText);
                    },
                    success: function(data) {
                        response($.map(data, function(c) {
                            return {
                                label: c.Company,
                                value: c.ID
                            }
                        }));
                    }
                });
            },
            minLength: 2,
            select: function(event, ui) {
                alert('Select');
            }
        });

ASP MVC:

    [AcceptVerbs(HttpVerbs.Post)]
    public JsonResult Search(string term)
    {
        if (term == null)
            term = "";

        List<JSON_Customer> customers = repCustomer.FindCustomers(term).ToList();
        return Json(customers);
    }

    public class JSON_Customer
    {
        public int ID { get; set; }
        public string Company { get; set; }
    }

    public IQueryable<JSON_Customer> FindCustomers(string searchText)
    {
        return from c in _db.Customers
               where c.Company.Contains(searchText)
               orderby c.Company
               select new JSON_Customer
               {
                   ID = c.ID,
                   Company = c.Company
               };
    }

我从 $.ajax 获取请求,并根据搜索词返回正确的客户列表。并且调用 success 方法。我可以看到 data 有一个 [object Object] 值,但接下来我该怎么办?没有客户出现在我的列表中。我正在使用 http://jqueryui.com/demos/autocomplete/#remote-jsonp 但它就是行不通。

有人知道为什么吗?

I'm trying to get the jQuery Automcomplete thing to work, but it wont do as i want :P
This is my code:

JavaScript:

        $("#CustomerID").autocomplete({
            source: function(request, response) {
                $.ajax({
                    type: "POST",
                    url: "/customer/search",
                    dataType: "json",
                    data: {
                        term: request.term
                    },
                    error: function(xhr, textStatus, errorThrown) {
                        alert('Error: ' + xhr.responseText);
                    },
                    success: function(data) {
                        response($.map(data, function(c) {
                            return {
                                label: c.Company,
                                value: c.ID
                            }
                        }));
                    }
                });
            },
            minLength: 2,
            select: function(event, ui) {
                alert('Select');
            }
        });

ASP MVC:

    [AcceptVerbs(HttpVerbs.Post)]
    public JsonResult Search(string term)
    {
        if (term == null)
            term = "";

        List<JSON_Customer> customers = repCustomer.FindCustomers(term).ToList();
        return Json(customers);
    }

    public class JSON_Customer
    {
        public int ID { get; set; }
        public string Company { get; set; }
    }

    public IQueryable<JSON_Customer> FindCustomers(string searchText)
    {
        return from c in _db.Customers
               where c.Company.Contains(searchText)
               orderby c.Company
               select new JSON_Customer
               {
                   ID = c.ID,
                   Company = c.Company
               };
    }

I get the request from $.ajax and I return the correct list of customers according to the search term. And the success method is invoked. I can see that data has a [object Object] value but what do I do next? No customers drops down in my list. I'm using the response($.map... code from the http://jqueryui.com/demos/autocomplete/#remote-jsonp but it just wont work.

Anyone know why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

み格子的夏天 2024-10-01 12:53:37

我在第一次 AJAX 请求之前使用它——我打赌它会有所帮助。定义标准项目并处理微软作为顶级属性放入的“d”属性。

  $.ajaxSetup({
     type: "POST",
     contentType: "application/json; charset=utf-8",
     data: "{}",
     dataFilter: function(data) {
        var msg;

        if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function')
           msg = JSON.parse(data);
        else
           msg = eval('(' + data + ')');

        if (msg.hasOwnProperty('d'))
           return msg.d;
        else
           return msg;
     }
  });

I use this before my first AJAX request -- I bet it will help. Defines the standard items and takes care of the "d" attribute microsoft puts in as the top level attribute.

  $.ajaxSetup({
     type: "POST",
     contentType: "application/json; charset=utf-8",
     data: "{}",
     dataFilter: function(data) {
        var msg;

        if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function')
           msg = JSON.parse(data);
        else
           msg = eval('(' + data + ')');

        if (msg.hasOwnProperty('d'))
           return msg.d;
        else
           return msg;
     }
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文