Kendo DataSource解析JSON不确定

发布于 2025-01-21 22:51:40 字数 2163 浏览 2 评论 0原文

我尝试将远程数据源绑定到我的Kendo Latpownist列表,但是Kendo DataSource可以正确解析JSON响应。结果证明是“未定义的”,并识别数据中的每个charachter作为选择。我不知道该错误在哪里,请帮助我!

控制器的响应:

“ [{“ code_id”:“ a”,“ code_name”:“ free”},{“ code_id”:“ b”,“ code_name”:“ borrowed”},{“ code_id”: “ code_name”:“无法”},{“ code_id”:“ c”,“ code_name”:“要借用”}]

:“无法 noreferrer“> html结果

的前端JS代码

$(document).ready(function () {
    $("#search_category").kendoDropDownList({
        dataTextField: "CODE_NAME",
        dataValueField: "CODE_ID",
        dataSource: {
            transport: {
                read: {
                    type: "GET",
                    url: "/BookData/GetDataList",
                    data: { name: "category" },
                    dataType: "json"
                }
            },
        },
    });
}

kendo下拉列表bookdatacontrollor

[HttpGet()]
public JsonResult GetDatalist(string name)
{
    string result;
    try
    {
        switch (name)
        {
            case "category":
                result = codeService.GetDataList("CODE_ID, CODE_NAME",
                "BOOK_CODE", "CODE_TYPE = 'BOOK_STATUS'");
                break;
            default:
                return Json(false);
         }
         return Json(result, JsonRequestBehavior.AllowGet);
    }
    catch (Exception ex)
    {
        return Json(false);
    }
}

后端class codeService

private DataTable GetDataTable(string sql)
{
    DataTable dt = new DataTable();
    using (SqlConnection conn = new SqlConnection(this.GetDBConnectionString()))
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd);
        sqlAdapter.Fill(dt);
        conn.Close();
    }

    return dt;
}

public string GetDataList(string col, string src, string cond)
{
    DataTable dt = GetDataTable(string.Format(@"SELECT {0} FROM {1} WHERE {2}",
                                    col, src, cond).ToUpper());
    string result = JsonConvert.SerializeObject(dt);
    return result;
}

I tried to binding remote DataSource for my Kendo DropDownList, but the Kendo Datasource could parse the json response correctly. The result turns out to be "undefined" and recognize every single charachter in the data as a selection. I have no idea where the bug is, please help me!

Responce from controllor:

"[{"CODE_ID":"A","CODE_NAME":"Free"},{"CODE_ID":"B","CODE_NAME":"Borrowed"},{"CODE_ID":"U","CODE_NAME":"Unable"},{"CODE_ID":"C","CODE_NAME":"To be Borrowed"}]"

HTML Result

Front-end js code for Kendo DropDownList

$(document).ready(function () {
    $("#search_category").kendoDropDownList({
        dataTextField: "CODE_NAME",
        dataValueField: "CODE_ID",
        dataSource: {
            transport: {
                read: {
                    type: "GET",
                    url: "/BookData/GetDataList",
                    data: { name: "category" },
                    dataType: "json"
                }
            },
        },
    });
}

BookDataControllor

[HttpGet()]
public JsonResult GetDatalist(string name)
{
    string result;
    try
    {
        switch (name)
        {
            case "category":
                result = codeService.GetDataList("CODE_ID, CODE_NAME",
                "BOOK_CODE", "CODE_TYPE = 'BOOK_STATUS'");
                break;
            default:
                return Json(false);
         }
         return Json(result, JsonRequestBehavior.AllowGet);
    }
    catch (Exception ex)
    {
        return Json(false);
    }
}

back-end class CodeService

private DataTable GetDataTable(string sql)
{
    DataTable dt = new DataTable();
    using (SqlConnection conn = new SqlConnection(this.GetDBConnectionString()))
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd);
        sqlAdapter.Fill(dt);
        conn.Close();
    }

    return dt;
}

public string GetDataList(string col, string src, string cond)
{
    DataTable dt = GetDataTable(string.Format(@"SELECT {0} FROM {1} WHERE {2}",
                                    col, src, cond).ToUpper());
    string result = JsonConvert.SerializeObject(dt);
    return result;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失而复得 2025-01-28 22:51:40

您正在返回code_idcode_name在您的控制器中,但是您在前端引用的值是category_name 。更改它们以引用正确的列名:

$('#search_category').kendoDropDownList({
  dataTextField: 'CODE_NAME',
  dataValueField: 'CODE_ID',
  dataSource: {
    transport: {
      read: {
        type: 'GET',
        url: '/BookData/GetDataList',
        data: { name: 'category' },
        dataType: 'json',
        success: function (response) {
          console.log(response);
        }
      }
    }
  }
});

You are returning CODE_ID and CODE_NAME in your controller, but the values you're referencing on the front-end are Category_Name and Category_ID. Change them to reference the correct column names:

$('#search_category').kendoDropDownList({
  dataTextField: 'CODE_NAME',
  dataValueField: 'CODE_ID',
  dataSource: {
    transport: {
      read: {
        type: 'GET',
        url: '/BookData/GetDataList',
        data: { name: 'category' },
        dataType: 'json',
        success: function (response) {
          console.log(response);
        }
      }
    }
  }
});
明月松间行 2025-01-28 22:51:40

似乎即使将数据返回json(结果)作为jsonresult,响应内容也可能是一个字符串,数据源无法以正确的方式处理。

尝试强加于使用分析方法:

$(document).ready(function () {
    $("#search_category").kendoDropDownList({
        dataTextField: "CODE_NAME",
        dataValueField: "CODE_ID",
        dataSource: {
            transport: {
                read: {
                    type: "GET",
                    url: "/BookData/GetDataList",
                    data: { name: "category" },
                    dataType: "json"
                }
            },
            schema: {
                parse: function(data) {
                    // Here you can check what 'data' really is
                    console.log(data);
                    return JSON.parse(data);
                }
            }
        },
    });
}

It seems that even returning your data with Json(result) as JsonResult, the response content could be a string, which the DataSource is not handling the right way.

Try enrusing that it will be json with parse method:

$(document).ready(function () {
    $("#search_category").kendoDropDownList({
        dataTextField: "CODE_NAME",
        dataValueField: "CODE_ID",
        dataSource: {
            transport: {
                read: {
                    type: "GET",
                    url: "/BookData/GetDataList",
                    data: { name: "category" },
                    dataType: "json"
                }
            },
            schema: {
                parse: function(data) {
                    // Here you can check what 'data' really is
                    console.log(data);
                    return JSON.parse(data);
                }
            }
        },
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文