DotNetBeginner
DotNetBeginner

Reputation: 439

Autocomplete using JSON

In my view i have

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

My jquery is

$(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 I 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");   

    result = result.Where(r => r.Contains(term)).ToList();         

    return Json(result , JsonRequestBehavior.AllowGet);
}  

I included a jquery library

<script src='<%: Url.Content("~/Scripts/jQueryUI/jquery-1.4.2.min.js") %>'    type="text/javascript"></script>  
<script src='<%: Url.Content("~/Scripts/jQueryUI/jquery-ui-1.8.2.custom.min.js") %>'  type="text/javascript"></script>

now where am i going wrong. All I see a a text box , no behavior of auto complete.

Upvotes: 0

Views: 185

Answers (1)

Moby&#39;s Stunt Double
Moby&#39;s Stunt Double

Reputation: 2550

The jQuery UI Team did not add support for jQuery 1.4.3 even until version 1.8.6 of UI (see here). Therefore, although you may have other issues going on, you also have a potential library incompatibility.

Upgrade your versions of both libraries and see where that takes you first.

http://jquery.com/download/ http://jqueryui.com/download/ Or https://developers.google.com/speed/libraries/devguide#jquery

I hope this helps.

Upvotes: 2

Related Questions