我正在使用带有EF6
的ASP.NET MVC。在Ajax调用搜索时,each()
方法返回undefined
,尽管每个数组的length
都是正确的。
$.ajax({
type: "GET",
url: "something",
contentType: "html",
success: function (result) {
//Receive the filtering data from controller and show it for user
SetData.append('<p>' + result.length+'</p>') //It's working correctly
$.each(result, function (index, value) {
var Data = "<p>" + value.supervisorName + "</p>"; // return undefined
console.log(value.supervisorName) //also return undefined
SetData.append(Data);
});
}
});
controller
方法
public JsonResult GetSearchingData(string SearchBy, string SearchValue)
{
Entities5 db = new Entities5();
List<Supervisors> StuList = new List<Supervisors>();
StuList = db.Supervisors.Where(x => x.supervisorName.StartsWith(SearchValue) || SearchValue == null).ToList();
var subCategoryToReturn = StuList.Select(S => new { Name = S.supervisorName });
return Json(subCategoryToReturn, JsonRequestBehavior.AllowGet);
}
答案 0 :(得分:0)
将Name更改为supervisorName
后,它正在工作var subCategoryToReturn = StuList.Select(S => new { supervisorName = S.supervisorName });