ASP.Net MVC Jquery UI自动完成 – 我调试时没有显示列表

我已在我的应用中为邮政编码实施了自动填充function。 我在Firebug中进行调试,我在控制台中看到操作正在执行,我在结果列表中得到了一个邮政编码列表,但是当我调试时,实际列表没有显示。

这是我的Customers控制器中的操作:

//the autocomplete request sends a parameter 'term' that contains the filter public ActionResult FindZipCode(string term) { string[] zipCodes = customerRepository.FindFilteredZipCodes(term); //return raw text, one result on each line return Content(string.Join("\n", zipCodes)); } 

这是标记(缩写)

    

这是我加载脚本的顺序:

       $(document).ready(function() { $("#ZipCodeID").autocomplete({ source: ''}); });  

有什么明显我想念的吗? 就像我说脚本正在抓取邮政编码列表一样,我测试时它们不会显示在我的页面上。

编辑:我添加了一个图像,显示我在firebug中看到的内容 – 看起来我收到了我的邮政编码,但是不会显示下拉列表。

我还更新了我的文本框,以便它在ui-widget div中,如下所示:

 

这是我正在使用的脚本:

  $(document).ready(function() { $("#ZipCodeID").autocomplete(''); });  

我能够使用以下代码获取自动完成建议:

控制器:

 public JsonResult FindZipCode(string term) { VetClinicDataContext db = new VetClinicDataContext(); var zipCodes = from c in db.ZipCodes where c.ZipCodeNum.ToString().StartsWith(term) select new { value = c.ZipCodeID, label = c.ZipCodeNum}; return this.Json(zipCodes, JsonRequestBehavior.AllowGet); } 

标记:

  

几个月前首次设置自动完成时,我遇到了很多问题。 例如,像你这样简单的默认连接从来没有对我有用。 我必须指定所有内容并将结果函数附加到它。

这可以100%工作,但它可能不适合你。 但我希望它有所帮助。 将两者放在document.ready()函数中。

 $("#products").autocomplete('<%:Url.Action("GetProducts", "Product") %>', { dataType: 'json', parse: function (data) { var rows = new Array(data.length), j; for (j = 0; j < data.length; j++) { rows[j] = { data: data[j], value: data[j].Title, result: data[j].Title }; } return rows; }, formatItem: function (row, y, n) { return row.PrettyId + ' - ' + row.Title + ' (' + row.Price + ' €)'; }, width: 820, minChars: 0, max: 0, delay: 50, cacheLength: 10, selectFirst: true, selectOnly: true, mustMatch: true, resultsClass: "autocompleteResults" }); $("#products").result(function (event, data, formatted) { if (data) { var item = $("#item_" + data.PrettyId), edititem = $("#edititem_" + data.PrettyId), currentQuantity; // etc... } }); 

尝试从控制器操作返回JSON:

 public ActionResult FindZipCode(string term) { string[] zipCodes = customerRepository.FindFilteredZipCodes(term); return Json(new { suggestions = zipCodes }, JsonRequestBehavior.AllowGet); } 

另外,不要忘记包含默认CSS,否则您可能看不到建议div。