简体   繁体   English

Kendo ui网格下拉编辑器不加载数据

[英]Kendo ui grid dropdown editor does not load data

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. 城市模型具有CityName(字符串),CityId(整数)和CityPostalCode(字符串)字段。 The only error in console is "Uncaught TypeError: e.slice is not a function" 控制台中的唯一错误是“未捕获的TypeError:e.slice不是函数”

upd* Code for PrepareCityModelForList upd * PrepareCityModelForList的代码

    protected virtual CompanyCityModel PrepareCityModelForList(City city)
    {

        return new CompanyCityModel()
        {
            CityId = city.Id,
            CityName = city.Name,
            PostalCode = city.PostalCode
        };
    }

upd*: returned JSON 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}

The problem was in json, that was passed by ajax. 问题出在json,由ajax传递。 You have to ensure your json is simple like: 您必须确保您的json很简单,例如:

      {[
          {"CityId":3,"CityName":"Minsk","PostalCode":"220000"}, 
            "CityId":4,"CityName":"Brest","PostalCode":"224000"}, 
            "CityId":5,"CityName":"Vitebsk","PostalCode":"210000"}, 
            "CityId":6,"CityName":"Gomel","PostalCode":"246000"}
      ]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM