【问题标题】:ScriptIgnore attribute not working on partial classScriptIgnore 属性不适用于部分类
【发布时间】:2014-03-25 17:00:09
【问题描述】:

我在使用局部视图上的 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

【问题讨论】:

    标签: asp.net-mvc json serialization scriptignore


    【解决方案1】:

    试试[ScriptIgnore(ApplyToOverrides = true)]

    【讨论】:

    • 这对我不起作用,即使使用上述方法装饰元数据中的属性,我仍然会收到循环引用错误,还有其他想法吗?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      • 2022-11-10
      • 2017-03-15
      • 2016-07-16
      • 1970-01-01
      • 2010-10-25
      相关资源
      最近更新 更多