0

我的查看代码:

脚本和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>

输入文本:

@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 
    });
}

控制器方法:

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

    return Json(houses);
}

我调试了javascript并选择了房屋..但结果未显示在自动完成中。为什么?

4

1 回答 1

1

我真的不认为你应该这样做。如果您需要自定义逻辑,请在autocomplete方法上使用回调:

$(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);
            });                
        }
    });
});
于 2012-04-04T16:26:29.840 回答