當控制器返回json結果時,對象名似乎缺失,我通常不介意,但flexbox jquery插件需要特定格式的json結果。返回對象名爲MVC的Json結果
Flexcombobox預期格式
{"results":[
{"id":"1","name":"Ant"},
{"id":"2","name":"Bear"},
{"id":"3","name":"Cat"},
{"id":"4","name":"Dog"},
{"id":"5","name":"Elephant"},
{"id":"6","name":"Fox"},
{"id":"7","name":"Guinea Pig"},
{"id":"8","name":"Horse"},
{"id":"9","name":"Iguana"},
{"id":"10","name":"Jaguar"}
]}
類
Public Class FlexboxResult
Private _id As String
Public Property Id() As String
Get
Return _id
End Get
Set(ByVal value As String)
_id = value
End Set
End Property
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
End Class
控制器代碼
Function PartYearsList() As JsonResult
Dim yearSelectList As List(Of FlexboxResult) = New List(Of FlexboxResult)
For index As Integer = DateTime.Now.Year To 1955 Step -1
yearSelectList.Add(New FlexboxResult() With {.Id = index, .Name = index})
Next
Return Me.Json(yearSelectList.ToArray(), JsonRequestBehavior.AllowGet)
End Function
JSON結果返回(縮短)
[{"Id":"2010","Name":"2010"},{"Id":"2009","Name":"2009"},{"Id":"2008","Name":"2008"}]
期望的結果(縮短)
{"results": [{"Id":"2010","Name":"2010"},{"Id":"2009","Name":"2009"},{"Id":"2008","Name":"2008"}]}
Flexcombobox文檔 http://www.fairwaytech.com/flexbox.aspx
對於那些對VB代碼感興趣的人。返回Json(New With {Key .results = yearSelectList},JsonRequestBehavior.AllowGet) – Allfocus 2010-10-29 11:24:47