如何将 kendo 网格的列表 <string> 转换为 JSON

How to convert List<string> to JSON for kendo grid

数据查询部分,我return数据为List<Customer>

SqlDataAdapter da = new SqlDataAdapter();    
da.SelectCommand = cmd;    
ds = new DataSet();    
da.Fill(ds);    
custlist = new List<Customer>();    
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)    
{    
    Customer cobj = new Customer();    
    cobj.CustomerID = Convert.ToInt32(ds.Tables[0].Rows[i]["CustomerID"].ToString());    
    cobj.Name = ds.Tables[0].Rows[i]["Name"].ToString();    
    cobj.Address = ds.Tables[0].Rows[i]["Address"].ToString();    
    cobj.Mobileno = ds.Tables[0].Rows[i]["Mobileno"].ToString();    
    cobj.EmailID = ds.Tables[0].Rows[i]["EmailID"].ToString();    
    cobj.Birthdate = Convert.ToDateTime(ds.Tables[0].Rows[i]["Birthdate"].ToString());    

    custlist.Add(cobj);    
}    
return custlist;

API部分,代码如下

Customer objCustomer = new Customer();    
DataAccessLayer objDB = new DataAccessLayer(); //calling class DBdata

// What do I need to do here next for my api to convert List<string> to json Matches kendo grid
// I tried with code but still not working => string jsonString = JsonSerializer.Serialize(objCustomer);
objCustomer.ShowallCustomer = objDB.Selectalldata();

对于Kendo UI部分,代码如下:

<div id="grid"></div>

<script>
    $(document).ready(function () {
        
        dataSource = new kendo.data.DataSource({
            transport: {
                read:  {
                    url: "/api/handler.ashx?tbl=like&func=getall", //api call code as above
                    dataType: "json"
                },                        
                parameterMap: function(options, operation) {
                    if (operation !== "read" && options.models) {
                        return {models: kendo.stringify(options.models)};
                    }
                }
            },
            batch: true,
            pageSize: 20,           
        });

        $("#grid").kendoGrid({ //kendo grid ui
            dataSource: dataSource,
            pageable: true,
            height: 550,
            toolbar: ["create"],            
            editable: "inline"
        });
    });

    function customBoolEditor(container, options) {
        $('<input class="k-checkbox" type="checkbox" name="Discontinued" data-type="boolean" data-bind="checked:Discontinued">').appendTo(container); //Kendo grid custom
    }
</script>

问题: API return数据列表为JSON,但还是没有输出

希望大家帮帮我。非常感谢。

我假设您已经确认 JSON 正在访问您的浏览器。如果没有,请首先确保您确实在获取数据。 您可能必须 定义数据源模式 才能正常工作。查看 this link 以获得与您的代码类似的完整示例。

            transport: {
                read: "/api/handler.ashx?tbl=like&func=getall"
            },
            schema: {
                model: {
                    fields: {
                        OrderID: { type: "number" },
                        Freight: { type: "number" },
                        ShipName: { type: "string" },
                        OrderDate: { type: "date" },
                        ShipCity: { type: "string" }
                    }
                }
            }

如果您希望动态生成架构,请查看 this link