0

My View Code:

Script and Css:

<link href="@Url.Content("~/Content/jquery-ui-1.8.18.custom.css")" rel="stylesheet"type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.18.custom.min.js")" type="text/javascript"></script>

Input text:

@Html.TextBoxFor(model => model.Filter.HouseName, new { style = "width: 205px", onKeyUp = "updateHouseNames()" })

Javascript:

function updateHouseNames() {
    var houseArray = new Array();

    $.post('@Url.Action("LoadHouses")', { houseName: $("#Filter_HouseName").val() }, function(houses) {
        houseArray = houses;
    });

    $("#Filter_HouseName").autocomplete({
        source: houseArray 
    });
}

Controller method:

[HttpPost]
public JsonResult LoadHouses(string houseName)
{
    var houses = this.HouseService.SelectByName(houseName).Select(e => new String(e.Name.ToCharArray())).ToArray();

    return Json(houses);
}

I debug the javascript and the houses are selected.. but the results are not displayed in autocomplete. Why?

0

1 Answer 1

1

I don't really think you should be doing it this way. If you need to customize the logic then use a callback on the autocomplete method:

$(function () {
    $('#Filter_HouseName').autocomplete({
        minLength: 1,
        source: function (request, response) {
            var term = request.term;
            var houseArray = new Array();

            $.post('@Url.Action("LoadHouses")', { houseName: term }, function(houses) {
                houseArray = houses;
                response(houseArray);
            });                
        }
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

Why the way i did doesn't work? I used this exemple: jqueryui.com/demos/autocomplete
You used which example? There are many examples on that one page.
The first one. "Default Functionality".
They are using a static array that does not change in that example. They are also only wiring up the autocomplete event once. You are doing it multiple times which is probably not good.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.