剑道 UI 下拉列表不接受 JSON 数据



使用 Kendo DropdownList for MVC,尝试确定 DropdownList 不接受此数据的原因。

@(Html.Kendo().DropDownList()
         .Name("CompanyList") //The name of the DropDownList is mandatory. It specifies the "id" attribute of the widget.
         .DataTextField("Table")
         .DataValueField("COM_NAME")
         .DataSource(source =>
        {
            source.Custom()   //  Read(read =>
            .Type("json")
            .Transport(transport =>
                {
                    transport.Read("GetallCompanies", "Home");
                })
            .ServerFiltering(true); //If true, the DataSource will not filter the data on the client.
        })
        .SelectedIndex(0) //Select the first item.

(

来自 SQL 操作的原始数据具有以下格式:

"{\"Table\":[{\"ORG_ID\":265498,\"COMPORGID\":239597,\"COM_NAME\":\"ABC Rentals \"},{\"ORG_ID\":164929,\"COMPORGID\":239698,\"COM_NAME\">

:\"Asbury Machine Shop \"}]}">

参考了剑道文档和其他 SO 示例。 将JSON放入验证器工具中,说其格式正确。

在页面中,下拉列表有一个左大括号,{作为顶部项目,单击时有几十个:未定义。

DataTextField 被称为"Table",因为 JSON 数组中的"Table",但它被设置为 COM_NAME。 控制器方法,

 [HttpGet]
    public JsonResult GetallCompanies()
    {
        var ddx = CompInfo.GetAllCompanies(); //returns dataset
        string thedata = JsonConvert.SerializeObject(ddx);
        return Json(thedata, JsonRequestBehavior.AllowGet);
    }

我认为在返回客户端之前,您不需要在ddxJson方法上使用SerializeObject。您可以尝试将GetAllCompanies更改为:

[HttpGet]
public JsonResult GetallCompanies()
{
    var ddx = CompInfo.GetAllCompanies(); //returns dataset
    //string thedata = JsonConvert.SerializeObject(ddx);
    return Json(ddx);
}

Json方法摘要:

创建一个 Microsoft.AspNetCore.Mvc.JsonResult 对象,该对象将指定的数据对象序列化为 JSON。

试试这个:

  1. 服务器端:将JsonConvert.SerializeObject行注释掉。

  2. 客户端:将以下内容添加到 DropDownList 配置中(例如,在 .SelectedIndex(0) 之后(:

    .Schema(schema => schema.Data("Table"))

最新更新