使用ASP MVC自动完成jQuery UI
我正试图让jQuery AutomcomComplete工作,但它不会像我想的那样:p 这是我的代码: JavaScript:使用ASP MVC自动完成jQuery UI,jquery,asp.net-mvc,jquery-ui,jquery-autocomplete,Jquery,Asp.net Mvc,Jquery Ui,Jquery Autocomplete,我正试图让jQuery AutomcomComplete工作,但它不会像我想的那样:p 这是我的代码: JavaScript: $("#CustomerID").autocomplete({ source: function(request, response) { $.ajax({ type: "POST", url: "/customer/s
$("#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方法。我可以看到数据有一个[object]值,但接下来我该怎么做?我的名单上没有客户。我正在使用响应$.map。。。源代码,但它就是不起作用
有人知道为什么吗?我在第一次请求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;
}
});
你能使用诊断/调试工具,如Firebug插件用于FF、开发者工具F12用于IE或Ctrl+Shift+J用于Chrome,准确查看返回到浏览器的内容以及如何处理它吗?哇,谢谢!它成功了,现在一切都正常了。我必须删除contentType:application/json;但是charset=utf-8,因为当我使用它时,Searchstring术语中的术语为null。不客气。这最初是为了与ASP.NET 2.0一起使用而编写的-可能是较新的DLL不需要contentType。。。我不确定。