我正在尝试遵循this article,但是我的程序似乎未定义任何项目列表。我已经设置了脚本(并通过插入alert()
来确保其正常工作)
$(document).ready(function () {
$("#cname").autocomplete(
{
source: function (request, response) {
$.ajax({
url: "/Contacts/Index",
type: "POST",
dataType: "json",
data: { Prefix: request.term },
success: function (data) {
response($.map(data, function (item) {
return { label: item.Name, value: item.Name };
}))
}
})
},
});
})
我已经有一个ContactsController
。在其中添加了文章建议的内容,
[HttpPost]
public JsonResult Index(string Prefix)
{
//Note : you can bind same list from database
var contacts = _context.Contacts.ToList();
var names = (from N in contacts
where N.Name.StartsWith(Prefix)
select new { N.Name });
return Json(names, JsonRequestBehavior.AllowGet);
}
页面加载后,当我单击/关注适当的输入文本框时,控制台将引发错误,
我查看了源代码,它似乎在jquery的这一行中断了,
_value: function()
{
return this.valueMethod.apply(this.element, arguments);
}
(这就是为什么我得出结论,我的行动没有达到的原因,因为apply
没什么可做的)。对这里可能发生的事情有任何想法吗?
我的Contact
模型如下:
[HandleError]
public class Contact
{
public int Id { get; set; }
[Required]
[StringLength(255)]
[Display(Name = "Contact Name ...")]
public string Name { get; set; }
[Required]
[StringLength(255)]
[Display(Name = "Contact Type")]
public string ContactType { get; set; }
[Required]
[Display(Name = "Phone:")]
public long ContactNumber { get; set; }
[Column(TypeName = "date")]
[Display(Name = "Birthdate")]
public DateTime BirthDate { get; set; }
}