简体   繁体   中英

At.js @mention - C# Web API

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);
            });
        }
    }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM