Json在序列化类型的对象时检测到循环引用?»

时间:2013-09-02 13:34:28

标签: asp.net vb.net asp.net-mvc-4 asp.net-ajax

我正在努力显示我的数据库中包含的竞赛列表,但每次出现此错误都会让我感到震惊:

  

在序列化类型为'WhereSelectListIterator 2[jTableSampleDatabaseLayer.Hik.JTable.Models.Concour,VB$AnonymousType_2 8 [System.String,System.Nullable 1[System.Int32],System.Nullable 1 [System.Int32],System.DateTime,System.Int32的对象时检测到循环引用, System.Int32,System.String,System.Int32]] ' on type 'System.Collections.Generic.IEnumerable 1 [jTableSampleDatabaseLayer.Hik.JTable.Models.Concour]``”。

这是我在vb中的代码:

Dim x As jTableSampleDatabaseLayer.totofootEntities = _
         New jTableSampleDatabaseLayer.totofootEntities
Dim dataSource = New MemoryDataSource()
Dim liste As List(Of Concour) = x.Concours.ToList()

dataSource.Concours.AddRange(liste)   

''ajout
Try
    Dim concours = dataSource.Concours
    Return Json(New With { _
        Key .Result = "OK", _
        Key .Records = concours _
    }, JsonRequestBehavior.AllowGet)

Catch ex As Exception
    Return Json(New With { _
        Key .Result = "ERROR", _
        Key .Message = ex.Message _
    }, JsonRequestBehavior.AllowGet)
End Try

如何避免此错误?

我尝试更改代码以避免此错误

现在我不再得到错误,但是我得到的结果是所有属性都等于null并且在我的数据库中注册的数量相同

  

{ “结果”: “OK”, “记录”:[{ “numero_concours”:NULL, “numero_etape”:0 “numero_type”:NULL, “numero_nature”:NULL, “titre_concours”:NULL,“date_de_sortie “:”/日期(-62135596800000)/ “ ”nbre_matchs“:0 ”nbre_rangs“:0, ”Etape酒店“:空, ”自然“:空, ”类型“:NULL, ”素数“:[],” RANGS “:[],” Recettes “:[],” RENCONTRES “:[]},{” numero_concours “:NULL,” numero_etape “:0”,numero_type “:NULL,” numero_nature “:NULL,” titre_concours“:空, “date_de_sortie”: “/日期(-62135596800000)/”, “nbre_matchs”:0 “nbre_rangs”:0, “Etape酒店”:空, “自然”:空, “类型”:NULL, “素数”: [], “RANGS”:[], “Recettes”:[], “RENCONTRES”:[]}]}``”。

新代码:

Dim x As jTableSampleDatabaseLayer.totofootEntities = New jTableSampleDatabaseLayer.totofootEntities
        Dim dataSource = New MemoryDataSource()
        Dim liste As List(Of Concour) = x.Concours.ToList()
        dataSource.Concours.AddRange(liste.Select(Function(p) New Concour))


        ''ajout
        Try

                     Dim concours = dataSource.Concours
            Return Json(New With {Key .Result = "OK", Key .Records = concours}, JsonRequestBehavior.AllowGet)

        Catch ex As Exception

            Return Json(New With {Key .Result = "ERROR", Key .Message = ex.Message}, JsonRequestBehavior.AllowGet)

        End Try

0 个答案:

没有答案