3

我在部分视图上使用 ScriptIgnore 标记来阻止属性序列化时遇到问题。

var docs = @Html.Raw(Json.Encode(Model))

有趣的是,当我将属性直接添加到 .tt 文件中的部分类时,它按预期工作,但是因为当我执行代码生成时该文件将被覆盖,所以我尝试使用 MetadataType

[MetadataType(typeof(DocumentMeta))] //this is added so we can add meta data to our partial class..
public partial class Document
{

}


[MetadataType(typeof(DocumentCategoryMeta))] //this is added so we can add meta data to our partial class..
public partial class DocumentCategory
{

}


public class DocumentMeta
{
    [ScriptIgnore]   //We add the scriptignore here because we are serializing some of these entities in client code
    public virtual ICollection<DocumentCategory> DocumentCategories { get; set; }
}

public class DocumentCategoryMeta
{
    [ScriptIgnore]        //We add the scriptignore here because we are serializing some of these entities in client code
    public virtual DocumentCategory Parent { get; set; }
}

我仍然遇到同样的错误:在序列化“DocumentCategory”类型的对象时检测到循环引用。

因为 DocumentCategory 包含分层数据。

任何帮助将不胜感激!

部落84

4

2 回答 2

1

尝试[ScriptIgnore(ApplyToOverrides = true)]

于 2014-07-14T10:36:34.967 回答
0

对于所有虚拟属性,您应该使用该ScriptIgnore属性,如下所示:[ScriptIgnore(ApplyToOverrides = true)]

于 2015-09-17T17:01:37.200 回答