循环引用和ScriptIgnore问题 [英] Circular References and ScriptIgnore problems

查看:950
本文介绍了循环引用和ScriptIgnore问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有称对方BusinessObject的几类,我需要序列之一的一个JsonResponse并将其返回给我的看法。我不断收到一个循环引用的异常,我无法摆脱它。我已经放在 [ScriptIgnore()] 装饰在每一个这不是一个简单数据类型物业财产,我仍然得到例外。我找不出问题出在哪里,因为我从刚才的一切阻止串行器,它仍然是在我吹起来。

I have several BusinessObject classes that refer to each other and I need to serialize one in a JsonResponse and return it to my view. I keep getting a circular reference exception and I cannot get rid of it. I have placed the [ScriptIgnore()] decorator on every property that is not a simple data type property and I am still getting the exception. I cant figure out where the problem is, as I am blocking the serializer from just about everything and it is still blowing up on me.

有什么办法,看看它们的序列化对象的当前状态是什么?

Is there any way to see what they current state of the serialized object is?

    [HttpPost]
    public JsonResult GetAnalysisInfo(int id)
    {
        ProjectContext myDB = db;
        SearchAnalysis searchanalysis = SearchAnalysis.Find(myDB, id);
        //searchanalysis.Results = searchanalysis.SearchResultsPrototype(myDB);
        return this.Json(searchanalysis);
    }

更新

我也试图实施了ISerializable无济于事。我GetObjectData使用非常简单:

Update

I have also tried implementing ISerializable to no avail. My GetObjectData is very simple:

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("SearchAnalysisId", this.SearchAnalysisId);
        info.AddValue("Created", this.Created);
    }

不过得到一个CircularRefernce错误。 DateTime数据类型不正确的序列化导致问题?

Still getting a CircularRefernce error. DateTime data types don't cause problems with Serialization right?

推荐答案

我做的,以prevent这个错误是返回反映这样我对象的属性一个匿名者键入的内容:

What I'm doing to prevent that error is to return an anonymouse type that reflects my object's properties like this :

    public JsonResult CompanyListJson()
    {
        var list = (from companies in appContext.Companies
                    where companies.Active.Equals(true)
                    select new
                    {
                        Name = companies.DbName,
                        Id = companies.Id,
                        Active = companies.Id.Equals(CurrentCompany.Id)

                    });
        return Json(list, JsonRequestBehavior.AllowGet);
    }

也许这是不是最好的方式,但它可以让我保持我的JSON薄,只推我需要的数据(并避免当然是循环引用除外)

Maybe it's not the best way, but it allows me to keep my JSON thin and push only the data I need ( and avoid that circular reference exception of course )

在你的样品看,我就从SearchAnalysis选择新的匿名者类型,以我所需要的属性。这应该工作

Looking at your sample, I would select new anonymouse type from SearchAnalysis, taking the properties I need. That should work

这篇关于循环引用和ScriptIgnore问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆