开发者问题收集

Kendo UI 下拉列表

2014-02-11
668

我有一个使用 mvc 包装器的 kendo 下拉列表

代码

    Html.Kendo.DropDownList() _
        .Name("MainCategories") _
        .HtmlAttributes(New With {.style = "width: 250px"}) _
        .DataTextField("CategoryName") _
        .OptionLabel("Select A Category") _
        .DataValueField("ID") _
        .DataSource(Function(source)
                        source.Read(Function(read) read.Action("GetCategories", "Home"))
                    End Function _
                    ) _
    .Render()


End Code

我在控制器中有一个函数

Public Function GetCategories() As String

    ' Dim dr As DataRow
    Dim dt As New DataTable
    Dim query As New LibQuery(LibSQL.ConString)
    query.OpenNoTran()
    Dim da As SqlDataAdapter = MainCategories.LoadAllMainCategoriesAdapt(query)
    da.Fill(dt)

    Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()

    Dim rows As New List(Of Dictionary(Of String, Object))()
    Dim row As Dictionary(Of String, Object)
    For Each dr As DataRow In dt.Rows
        row = New Dictionary(Of String, Object)()
        For Each col As DataColumn In dt.Columns
            row.Add(col.ColumnName, dr(col))
        Next
        rows.Add(row)
    Next
    Return serializer.Serialize(rows)

    '  Return Json(da, JsonRequestBehavior.AllowGet)

End Function

通过检查我返回了一个 json 结果

[{"ID":1,"CategoryName":"Hair Dressing"},
 {"ID":2,"CategoryName":"Gardening"},
 {"ID":3,"CategoryName":"Animal Care"},
 {"ID":4,"CategoryName":"Accounting"},
 {"ID":5,"CategoryName":"Cleaning"},
 {"ID":6,"CategoryName":"Automotive"},
 {"ID":7,"CategoryName":"Another"},
 {"ID":8,"CategoryName":"Costas Cooking"},
 {"ID":9,"CategoryName":"cvb"},
 {"ID":10,"CategoryName":"cvbcbvcbv"},
 {"ID":11,"CategoryName":"cxxxxx"},
 {"ID":12,"CategoryName":"MVC ABout Is Working"},
 {"ID":13,"CategoryName":"Another MvC"}]

由于某种原因,下拉列表中的所有列表值(datatextvalue)都是“未定义”的,这很奇怪,我使用自己的数据库连接文件,应该没问题。也许是因为我没有使用 JsonResult?有什么想法吗?

谢谢

1个回答

您目前似乎将 JSON 作为字符串返回 - 带有额外的引号,例如“[{"ID":1}]”。这是因为您自己对其进行序列化,而不是将其留给 Json() 方法。

尝试使用

Return Json(rows, JsonRequestBehavior.AllowGet)

而不是

Return serializer.Serialize(rows)
Atanas Korchev
2014-02-11