简体   繁体   English

KendoUI DropDownList如何绑定

[英]KendoUI DropDownList how to bind

Can someone please provide an example of how I can bind the following two properties to my BookType Kendo DropDown? 有人可以提供一个示例,说明如何将以下两个属性绑定到BookType Kendo DropDown吗? I need to bind the dataTextField to "Name" and dataValueField to "BookTypeId". 我需要将dataTextField绑定到“名称”,并将dataValueField绑定到“ BookTypeId”。

The data is coming back in the form of the following: 数据以以下形式返回:

books: Object
bookTypes: Array[4]
  0: Object
       ID: 1
       Name: "Web Development"
       BookTypeId: 1
  1: Object
       ID: 2
       Name: "Networking"
       BookTypeId: 2
  2: Object
       ID: 3
       Name: "Game Development"
       BookTypeId: 3
  3: Object
       ID: 4
       Name: "OS Development"
       BookTypeId: 4

Here is the call I'm making: 这是我正在拨打的电话:

$.ajax({
async: false,
type: "GET",
url: "GetBookTypes",
contentType: "application/json",
dataType: "json",
success: function (res) {
    if (typeof res !== "undefined" && typeof res.Errors !== "undefined") {
        if (res.Errors.length > 0) {
            // Display each error
            for (var i = 0; i < res.Errors.length; i++) {
                alert(res.Errors[i].ErrorMessage);
            }
        }
    } else if (typeof res !== "undefined") {

        $("#BookType").kendoDropDownList({
            dataTextField: "Name",
            dataValueField: "BookTypeId",
            dataSource: res,
        }).data("kendoDropDownList");
    }
},
complete: function () {

},
error: function (res) {
    alert(res.responseText);
}

}); });

You need to bind the array data itself to the datasource: 您需要将数组数据本身绑定到数据源:

$("#BookType").kendoDropDownList({ dataTextField: "Name", dataValueField: "BookTypeId", dataSource: res.bookTypes, });

See fiddle 见小提琴

But it is somewhat easier when you use remote datasource binding, like: 但是,当您使用远程数据源绑定时,它会更容易一些,例如:

$("#BookType").kendoDropDownList({ dataTextField: "Name", dataValueField: "BookTypeId", dataSource: { transport: { read: { dataType: "jsonp", url: "GetBookTypes", } } } });

But you need your action to return an array of book types directly. 但是您需要采取措施直接返回一系列书籍类型。

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

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