控制台显示此错误:
jquery-3.3.1.js:7972 Uncaught TypeError: Cannot read property '0' of undefined at val (jquery-3.3.1.js:7972) at add (jquery-3.3.1.js:8463) at buildParams (jquery-3.3.1.js:8450) at Function.jQuery.param (jquery-3.3.1.js:8483) at Function.ajax (jquery-3.3.1.js:9073) at $.<computed>.<computed>.source (Index:189353) at $.<computed>.<computed>._search (VM12 jquery-ui-1.12.1.js:6015) at $.<computed>.<computed>._search (VM12 jquery-ui-1.12.1.js:144) at $.<computed>.<computed>.search (VM12 jquery-ui-1.12.1.js:6007) at $.<computed>.<computed>.search (VM12 jquery-ui-1.12.1.js:144)
,当我开始打字时发送。
我的脚本是
$('#pesticideTypeInput').autocomplete({
source: function (request, response) {
$.ajax({
url: '@Url.Action("GetPesticideTypesList", "Search")',
dataType: "json",
data: { search: $('#pesticideTypeInput').val },
success: function (data) {
response($.map(data, function (item) {
//return { label: item.PesticideTypeAR, value: item.PesticideTypeAR };
return item;
}));
},
error: function (xhr, status, error) {
alert("Error");
}
});
}
});
在我的控制器中,我有:
public JsonResult GetPesticideTypesList(string term)
{
var pesticidesTypesList = db.PesticideTypes.Where(x => x.PesticideTypeAR.Contains(term)).ToList();
return new JsonResult {Data=pesticidesTypesList, JsonRequestBehavior=JsonRequestBehavior.AllowGet };
}
我的模型课是:
public partial class PesticideType
{
public PesticideType()
{
this.PesticidesInfoes = new HashSet<PesticidesInfo>();
}
public int PesticideTypePk { get; set; }
public string PesticideTypeAR { get; set; }
public virtual ICollection<PesticidesInfo> PesticidesInfoes { get; set; }
}
感谢您的帮助,谢谢!