【发布时间】: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