使用 ASP MVC 的 jQuery UI 自动完成
我正在尝试让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在第一次 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.