本文介绍了剑道UI格下拉编辑器不会加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
无法加载数据剑道下拉列表。它从后端获取数据,但列表为空。后端如下:
[HttpPost]
公众的ActionResult GetCities(DataSourceRequest命令)
{
VAR cityModel = _uow.Cities.GetAll()了ToList()。
VAR gridModel =新DataSourceResult
{
数据= cityModel.Select(prepareCityModelForList)
合计= cityModel.Count
}; 返回JSON(gridModel);
}
前端
模式:{
数据:数据,
总说:总,
错误:错误,
型号:{
ID:ID,
字段:{
名称:{编辑:真的,键入:串},
城市:{设置defaultValue:{CityId:0,CITYNAME:选择城市}},
地址:{编辑:真的,键入:串},
联系电话:{编辑:真的,键入:串},
传真:{编辑:真的,键入:串}, }
}
},
......
列: [...
{
现场:City.Name
标题:城市,
编辑:cityDropDownEditor,
模板:#= City.CityName#,
宽度:200
}
....
功能cityDropDownEditor(集装箱,期权){
$('<输入所需的数据文本字段=CITYNAME数据值场=CityId数据绑定=值:'+ options.field +'/>')
.appendTo(集装箱)
.kendoDropDownList({
autoBind:假的,
数据源: {
运输: {
读:
{
网址:@ Html.Raw(Url.Action(GetCities,联系方式)),
键入:POST,
数据类型:JSON
}
}
}
});
}
这个城市模型CITYNAME(串),CityId(int)和CityPostal code(字符串)领域。在控制台的唯一错误未捕获类型错误:e.slice不是一个函数
UPD * code为prepareCityModelForList
受保护的虚拟CompanyCityModel prepareCityModelForList(城市)
{ 返回新CompanyCityModel()
{
CityId = city.Id,
CITYNAME = city.Name,
邮政code = city.Postal code
};
}
UPD *:返回的JSON
<$p$p><$c$c>{\"ExtraData\":null,\"Data\":[{\"CityId\":3,\"CityName\":\"Minsk\",\"Postal$c$c\":\"220000\"},{\"CityId\":4,\"CityName\":\"Brest\",\"Postal$c$c\":\"224000\"},{\"CityId\":5,\"CityName\":\"Vitebsk\",\"Postal$c$c\":\"210000\"},{\"CityId\":6,\"CityName\":\"Gomel\",\"Postal$c$c\":\"246000\"}],\"Errors\":null,\"Total\":4}解决方案
问题是JSON,这是由AJAX通过。你必须确保你的JSON是简单的像:
{[
{CityId:3,城市名:明斯克,邮政code:220000},
CityId:4,城市名:布列斯特,邮政code:224000},
CityId:5,城市名:维捷布斯克,邮政code:210000},
CityId:6,城市名:戈梅利,邮政code:246000}
]}
Can not load data to kendo dropdown list. It gets data from backend but list is empty. BackEnd looks like:
[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);
}
Front end
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"
}
}
}
});
}
The city model has CityName (string), CityId (int) and CityPostalCode (string) fields. The only error in console is "Uncaught TypeError: e.slice is not a function"
upd* Code for PrepareCityModelForList
protected virtual CompanyCityModel PrepareCityModelForList(City city)
{
return new CompanyCityModel()
{
CityId = city.Id,
CityName = city.Name,
PostalCode = city.PostalCode
};
}
upd*: returned 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}
解决方案
The problem was in json, that was passed by ajax. You have to ensure your json is simple like:
{[
{"CityId":3,"CityName":"Minsk","PostalCode":"220000"},
"CityId":4,"CityName":"Brest","PostalCode":"224000"},
"CityId":5,"CityName":"Vitebsk","PostalCode":"210000"},
"CityId":6,"CityName":"Gomel","PostalCode":"246000"}
]}
这篇关于剑道UI格下拉编辑器不会加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!