asp.net-mvc - ScriptIgnore 属性不适用于部分类

标签 asp.net-mvc json serialization scriptignore

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

var 文档 = @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

最佳答案

尝试[ScriptIgnore(ApplyToOverrides = true)]

关于asp.net-mvc - ScriptIgnore 属性不适用于部分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22641475/

相关文章:

json - 快速错误 : Invalid top-level type in JSON write

c# - 将 JSON 反序列化为 C# 动态对象?

symfony - 使用 NormalizerAwareTrait 的 Normalizer 空引用

c# - 如何在 asp.net 中打印 UPS 运输服务标签

php - 未捕获的语法错误意外数字 JSON.parse

c# - 如何使用 ViewModel 模式在 MVC 中编辑多个表

ios - 如何暂停应用程序直到使用 Alamofire 下载 json 数据?

java - Python(和Java)中最快的数据打包

c# - 强制下载文件,默认不在浏览器上打开它

asp.net - .net core 2.1 从另一个位置重定向时,cookie 值始终为空