Costas Aletrari
Costas Aletrari

Reputation: 387

Kendo UI Dropdown List

i have a kendo dropdownlist that uses the mvc wrappers

code

    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

I have a function in a controller

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

and by checking im returning the a json result

[{"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"}]

for some reason all the list values(datatextvalue) are "undefined" in the dropdownlist which is strange,, im using my own database connection files which should be fine. maybe because im not using JsonResult?? any ideas.

thank you

Upvotes: 0

Views: 673

Answers (1)

Atanas Korchev
Atanas Korchev

Reputation: 30671

You currently seem to return a the JSON as a string - with extra quotes e.g. "[{"ID":1}]". This is because you are serializing it yourself instead of leaving it to the Json() method.

Try using

Return Json(rows, JsonRequestBehavior.AllowGet)

instead of

Return serializer.Serialize(rows)

Upvotes: 1

Related Questions