2014-02-11 152 views
0

我有一個使用MVC包裝劍道UI下拉列表

代碼

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 

並且通過檢查IM返回具有的功能的劍術下拉列表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) 
嘗試