0

以下 JsonResult 返回:

A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.IntegerList_76C57D0878505062AEA67E9EB9F708830FA20FDE4F13A53543EC2E6ED84C1A2C'

并根据调试器

'Children could not be evaluated'

为线

var integerLists = db.IntegerLists.Include("Integers");

如果我删除包含,这将成功运行。同样,如果我改为执行注释代码。

过去,我通过使用 [IgnoreDataMember] 装饰我的域模型来解决循环引用,但这次似乎不起作用。

控制器

    public JsonResult GetIntegerLists()
    {
        var integerLists = db.IntegerLists.Include("Integers");

        //var integerLists = new List<IntegerList>();
        //integerLists.Add(new IntegerList()
        //{
        //    IntegerListID = 1,
        //    Direction = "Ascending",
        //    Performance = 467,
        //    Integers = { new Integer { IntegerValue = 34 }, new Integer { IntegerValue = 5654 }, new Integer { IntegerValue = 343 } }
        //});
        //integerLists.Add(new IntegerList()
        //{
        //    IntegerListID = 2,
        //    Direction = "Ascending",
        //    Performance = 637,
        //    Integers = { new Integer { IntegerValue = 4 }, new Integer { IntegerValue = 646 }, new Integer { IntegerValue = 23 } }
        //});
        //integerLists.Add(new IntegerList()
        //{
        //    IntegerListID = 3,
        //    Direction = "Ascending",
        //    Performance = 235,
        //    Integers = { new Integer { IntegerValue = 64378 }, new Integer { IntegerValue = 1 }, new Integer { IntegerValue = 533 } }
        //});

        return Json(integerLists, JsonRequestBehavior.AllowGet);
    }

    [HttpPost]
    public ActionResult Index(IntegerList viewModel)
    {
        if (ModelState.IsValid)
        {
            db.IntegerLists.Add(viewModel);
            db.SaveChanges();
        }

        return View();
    }

楷模

public class Integer
{
    public int IntegerID { get; set; }
    public int IntegerValue { get; set; }
    public int IntegerListID { get; set; }
    public virtual IntegerList IntegerList { get; set; }
}

public class IntegerList
{
    public int IntegerListID { get; set; }
    public string Direction { get; set; }
    public long Performance { get; set; }
    public virtual ICollection<Integer> Integers { get; set; }
}
4

1 回答 1

0

您有一个导航属性返回到它所属的列表。您可能不想在序列化时使用它。使用ScriptIgnore属性。

public class Integer
{
    public int IntegerID { get; set; }
    public int IntegerValue { get; set; }
    public int IntegerListID { get; set; }
    [ScriptIgnore]
    public virtual IntegerList IntegerList { get; set; }
}
于 2013-11-13T18:05:35.723 回答