我正在尝试让 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-jsonpresponse($.map...
的代码,但它不起作用。
有谁知道为什么?