qinking126
qinking126

Reputation: 11885

jQueryUI autoComplete returns back empty list

In my asp.net mvc3 application, I added autoComplete for my search box. When I test it, there are 3 results returned back from the action. You can see a list showed up, however, this is an empty list, you only see 3 < li >< /li >, and there's nothing there between the li tag.

I pretty sure, action is fine, because, It did return 3 results back. I can verify that by seeing 3 empty < li > tag. what should I do to add name < li > apple < / li>

        $("#searchbox").autocomplete({
            source:"/Home/SearchIngredients",
            minLength: 2

        });


    public virtual JsonResult SearchIngredients(string term)
    {
        var ingredients = _smoothieService.GetIngredients(term);

        var data = ingredients.Select(x => new {Id = x.NDB_No, Value = x.Name}).Take(25).ToArray();
        return Json(data, JsonRequestBehavior.AllowGet);
    }

Upvotes: 3

Views: 5068

Answers (1)

Nathan
Nathan

Reputation: 2775

Something like this:

$('#search').autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "/Home/SearchIngredients",
            dataType: 'json',
            data: request,
            success: function (data) {
                response(data.map(function (value) {
                    return {
                        'label': '<li>' + value.Id + '</li>',
                        'value': value.Value
                    };  
                }));
            }   
        }); 
    },  
    minLength: 2
})

Upvotes: 12

Related Questions