Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my view i have

HTML
@Html.TextBoxFor(per => per.Hospital, new { style = "width:220px", @maxlength = "50", data_autocomplete = Url.Action("HospitalList", "Person") })


My jquery is

JavaScript
$(document).ready(function () {        
    $('input[data_autocomplete]').each(function () {
        var url = $(this).data('autocomplete');
        $(this).autocomplete({
            source: function (request, response) {
                $.getJSON(url, {
                    term: request.term
                }, response);
            }
        });
    });
});


And a created a new Action result

public ActionResult HospitalList(string term)
    {
        List<string> result = new List<string>();
        result.Add("Hospital 1");
        result.Add("NYUMC");
        result.Add("Christ");
        result.Add("Bellevue");
        result.Add("NewYork-Presbyterian");
        result.Add("North Central Bronx Hospital");            

        return Json(result , JsonRequestBehavior.AllowGet);
    }  

now where am i going wromg. All I see a a text box , no behavior of auto complete. Should i be including any jquery library for it to work
Posted

1 solution

This is because you have wrong autocomplete source, which is supposed to be an array, or a string or a function. In your case, the response function lacks return statement which is a main part defining the autocomplete data.

Please see: http://api.jqueryui.com/autocomplete/#option-source[^].

Pay attention for the sample "Using a custom source callback to match only the beginning of terms".

See also: http://api.jqueryui.com/autocomplete/[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900