【问题标题】:Json Result with DataAnnotations on Model in WebApiWebApi中模型上带有DataAnnotations的Json结果
【发布时间】:2013-08-12 19:02:18
【问题描述】:

我正在使用 ASP.NET WebApi,在将结果发送到 JSON 时,是否可以在模型 - ModelMetadata 上发送数据注释 例如 DisplayName 属性、 ShowInEdit 、 ShowInDispplay 和其他自定义属性以及 Fieldnmae 和 value。

public class Role
    {
        public Role()
        {
            Users = new List<User>();
        }

        [ScafoldingColumn(false)]
        public int RoleId { get; set; }

        [DisplayName("Role Name")]
        [Required]
        [ShowForDisplay(true)]
        [ShowForEdit(true)]
        public string RoleName { get; set; }

        [ShowForDisplay(true)]
        public ICollection<User> Users { get; set; }
    }

提前致谢

【问题讨论】:

  • 您能提供您的 C# 类和您想要的示例吗?

标签: c# javascript jquery asp.net-mvc asp.net-web-api


【解决方案1】:

JSON 作为文件格式不支持数据注释。到目前为止,我所看到的最好的解决方案是使用元操作。这些元操作可以将数据注释作为附加的 json 对象返回。之后您还可以将这些数据缓存在客户端或类似设备上的本地存储中,因此您无需在每次用户访问页面时下载它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-08
    • 2015-10-30
    • 2021-09-21
    • 2013-04-28
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多