+1  A: 

I had huge problems with autocomplete few months ago when first setting it up. For instance, the simple default wireup like you do it never worked for me. I had to specify everything and also attach the result function to it.

This works 100% but it might not be suitable for you. But I hope it helps. Put both in document.ready() function.

$("#products").autocomplete('<%:Url.Action("GetProducts", "Product") %>', {
    dataType: 'json',
    parse: function (data) {
        var rows = new Array(data.length), j;
        for (j = 0; j < data.length; j++) {
            rows[j] = { data: data[j], value: data[j].Title, result: data[j].Title };

        }
        return rows;
    },
    formatItem: function (row, y, n) {
        return row.PrettyId + ' - ' + row.Title + ' (' + row.Price + ' €)';
    },
    width: 820,
    minChars: 0,
    max: 0,
    delay: 50,
    cacheLength: 10,
    selectFirst: true,
    selectOnly: true,
    mustMatch: true,
    resultsClass: "autocompleteResults"
});
$("#products").result(function (event, data, formatted) {
    if (data) {

        var item = $("#item_" + data.PrettyId),
                    edititem = $("#edititem_" + data.PrettyId),
                    currentQuantity;
        // etc...
    }
});
mare
+1  A: 

Try returning JSON from your controller action:

public ActionResult FindZipCode(string term)
{
    string[] zipCodes = customerRepository.FindFilteredZipCodes(term);
    return Json(new { suggestions = zipCodes }, JsonRequestBehavior.AllowGet);
}

Also don't forget to include the default CSS or you might not see the suggestions div appear.

Darin Dimitrov
Thanks! I'm using the CSS that I got with the Theme I'm using (peppergrinder) and it's working for other Jquery ui stuff - like the accordian. What would the default CSS be? I don't have one of those...
Ben
Yes, by default I meant the one you got with the theme. Precisely.
Darin Dimitrov
I double checked my CSS and changed my action to retun JSON and it now returns JSON, but still does not show the suggestions div. In fact - I don't have a suggestions div - I just have the UI Widget div and the text input within. Should I have another div for suggestions to show or does the autocomplete functionality handle that?
Ben
+1  A: 

I was able to get the autocomplete suggestions working using the following code:

Controller:

public JsonResult FindZipCode(string term)
    {
        VetClinicDataContext db = new VetClinicDataContext();

        var zipCodes = from c in db.ZipCodes
                       where c.ZipCodeNum.ToString().StartsWith(term)
                       select new { value = c.ZipCodeID, label = c.ZipCodeNum};

        return this.Json(zipCodes, JsonRequestBehavior.AllowGet);
    }

Markup:

<script type="text/javascript">
    $(document).ready(function() {
        $("#ZipCodeID").autocomplete({
                  source: '<%= Url.Action("FindZipCode", "Customers") %>',
        });
    });
</script>

<div class="ui-widget"><input type="text" name="ZipCodeID" id="ZipCodeID" /></div>
Ben