2012-04-04 100 views
0

我查看代码: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

我真的不认为你应该这样做。如果您需要自定义逻辑,请在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); 
      });     
     } 
    }); 
}); 
+0

为什么我这样做的方式不起作用?我用这个例子:http://jqueryui.com/demos/autocomplete/ – 2012-04-04 16:52:54

+0

你用过哪个例子?这一页上有很多例子。 – Dismissile 2012-04-04 19:05:35

+0

第一个。 “默认功能”。 – 2012-04-05 11:59:17