使用JSON自动完成

时间:2013-04-05 20:02:59

标签: jquery asp.net-mvc json

在我看来,我有

@Html.TextBoxFor(per => per.Hospital, new { 
    style = "width:220px", @maxlength = "50", 
    data_autocomplete = Url.Action("HospitalList", "Person") })

我的jquery是

$(document).ready(function () {        
    $('input[data-autocomplete]').each(function () {
        var url = $(this).data('autocomplete');
        $(this).autocomplete({
            source: function (request, response) {
                $.getJSON(url, {
                    term: request.term
                }, response);
            }
        });
    });
});

我创建了一个新的Action结果

public ActionResult HospitalList(string term)
{
    List<string> result = new List<string>();
    result.Add("Hospital 1");
    result.Add("NYUMC");
    result.Add("Christ");
    result.Add("Bellevue");
    result.Add("NewYork-Presbyterian");
    result.Add("North Central Bronx Hospital");   

    result = result.Where(r => r.Contains(term)).ToList();         

    return Json(result , JsonRequestBehavior.AllowGet);
}  

我包含了一个jquery库

<script src='<%: Url.Content("~/Scripts/jQueryUI/jquery-1.4.2.min.js") %>'    type="text/javascript"></script>  
<script src='<%: Url.Content("~/Scripts/jQueryUI/jquery-ui-1.8.2.custom.min.js") %>'  type="text/javascript"></script>

现在我哪里出错了。我只看到一个文本框,没有自动完成的行为。

1 个答案:

答案 0 :(得分:2)

即使在UI的1.8.6版本之前,jQuery UI Team也没有添加对jQuery 1.4.3的支持(参见here)。因此,尽管您可能还有其他问题,但您也可能存在库不兼容的问题。

升级两个库的版本并查看第一个版本。

http://jquery.com/download/ http://jqueryui.com/download/ 要么 https://developers.google.com/speed/libraries/devguide#jquery

我希望这会有所帮助。