Jquery Autocomplete + ASP.NET MVC 3不显示结果

时间:2012-04-04 14:30:47

标签: c# jquery asp.net-mvc-3

我的观看代码:

脚本和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并选择了房屋..但结果不会显示在自动完成中。为什么呢?

1 个答案:

答案 0 :(得分: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);
            });                
        }
    });
});