无法将数据加载到kendo下拉列表。它从后端获取数据但列表为空。 BackEnd看起来像:
[HttpPost]
public ActionResult GetCities(DataSourceRequest command)
{
var cityModel = _uow.Cities.GetAll().ToList();
var gridModel = new DataSourceResult
{
Data = cityModel.Select(PrepareCityModelForList),
Total = cityModel.Count
};
return Json(gridModel);
}
前端
schema: {
data: "Data",
total: "Total",
errors: "Errors",
model: {
id: "Id",
fields: {
Name: { editable: true, type: "string" },
City: { defaultValue: { CityId: 0, CityName: "Select City" } },
Address: { editable: true, type: "string" },
Tel: { editable: true, type: "string" },
Fax: { editable: true, type: "string" },
}
}
},
......
columns: [...
{
field: "City.Name",
title: "City",
editor: cityDropDownEditor,
template: "#=City.CityName#",
width: 200
}
....
function cityDropDownEditor(container, options) {
$('<input required data-text-field="CityName" data-value-field="CityId" data-bind="value:' + options.field + '"/>')
.appendTo(container)
.kendoDropDownList({
autoBind: false,
dataSource: {
transport: {
read:
{
url: "@Html.Raw(Url.Action("GetCities", "Contact"))",
type: "POST",
dataType: "json"
}
}
}
});
}
城市模型具有CityName(字符串),CityId(int)和CityPostalCode(字符串)字段。控制台中唯一的错误是&#34; Uncaught TypeError:e.slice不是函数&#34;
upd * PrepareCityModelForList的代码
protected virtual CompanyCityModel PrepareCityModelForList(City city)
{
return new CompanyCityModel()
{
CityId = city.Id,
CityName = city.Name,
PostalCode = city.PostalCode
};
}
upd *:返回JSON
{"ExtraData":null,"Data":[{"CityId":3,"CityName":"Minsk","PostalCode":"220000"},{"CityId":4,"CityName":"Brest","PostalCode":"224000"},{"CityId":5,"CityName":"Vitebsk","PostalCode":"210000"},{"CityId":6,"CityName":"Gomel","PostalCode":"246000"}],"Errors":null,"Total":4}