jQuery UI的自动完成与ASP MVC [英] jQuery UI Autocomplete with ASP MVC
问题描述
我试图让jQuery的Automcomplete东西的工作,但它不会做的,因为我想:P
这是我的code:
I'm trying to get the jQuery Automcomplete thing to work, but it wont do as i want :P This is my code:
JavaScript的:
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:
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
,我根据搜索词回报客户的正确列表。和成功
方法被调用。我可以看到数据
有一个 [对象的对象]
的价值,但我该怎么做?没有客户在我的下拉列表。我使用了响应($。地图...
code从的 http://jqueryui.com/demos/autocomplete/#remote-jsonp ,但它只是不会工作。
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?
推荐答案
我我的第一个AJAX请求前使用此 - 我敢打赌,这将有助于。定义标准的项目,并采取了D的保健属性把微软作为顶级属性。
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;
}
});
这篇关于jQuery UI的自动完成与ASP MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!