I am using https://github.com/ichord/At.js library to achieve autocomplete.
But it shows a list of "undefined" dropdown when I am using remoteFilter like they said in https://github.com/ichord/At.js/wiki/How-to-use-remoteFilter .
Model:
public class CaseHistory
{
public int CaseHistoryId { get; set; }
[Display(Name = "Symptom/Disease")]
[Required(ErrorMessage = "Please enter symptom or disease")]
public string SymptomOrDisease { get; set; }
public string Description { get; set; }
}
API action code:
private ApplicationDbContext db = new ApplicationDbContext();
// GET api/CaseHistories
public IQueryable<CaseHistory> GetCaseHistories()
{
return db.CaseHistories;
}
Here is my code in the razor view:
var myUrl = 'https://localhost:44301/api/CaseHistories';
$('#inputor').atwho({
at: ":",
callbacks: {
/*
It function is given, At.js will invoke it if local filter can not find any data
query [String] matched query
callback [Function] callback to render page.
*/
remoteFilter: function(query, callback) {
$.getJSON(myUrl, { q: query }, function (data) {
callback(data);
});
}
}
});
Change the code in the controller to be:
public dynamic GetCaseHistories()
{
return db.CaseHistories.Select(x => x.SymptomOrDisease).ToList();
}
The issue is that the parameter you pass to callback should be array of strings.
If you really wanted to do this in js:
var myUrl = 'https://localhost:44301/api/CaseHistories';
$('#inputor').atwho({
at: ":",
callbacks: {
/*
It function is given, At.js will invoke it if local filter can not find any data
query [String] matched query
callback [Function] callback to render page.
*/
remoteFilter: function(query, callback) {
$.getJSON(myUrl, { q: query }, function (data) {
var targetData = [];
for(var i = 0;i < data.length;i++){
targetData.push(data[i].SymptomOrDisease);
}
callback(targetData);
});
}
}
});