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?