加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 在类上使用DisplayAttribute

发布时间:2020-12-15 08:34:19 所属栏目:百科 来源:网络整理
导读:因为我们可以使用System.ComponentModel.DataAnnotations.DisplayAttribute为属性设置标签,我想将它用于类,但不允许在类上使用它. using System.ComponentModel.DataAnnotations;[Display(Name = "A person")]public class Person{ [Display(Name = "A name"
因为我们可以使用System.ComponentModel.DataAnnotations.DisplayAttribute为属性设置标签,我想将它用于类,但不允许在类上使用它.
using System.ComponentModel.DataAnnotations;

[Display(Name = "A person")]
public class Person
{
    [Display(Name = "A name")]
    public string Name { get; set; }
}

有人知道解决方法吗?

编辑:
我想在强类型视图上使用它.当我创建一个新的强类型视图时,类名称是用HTML硬编码的,如下所示:

@model Models.Person
<fieldset>
    <legend>Person</legend>
    <div class="display-label">
        @Html.LabelFor(model => model.Name)
    </div>
</fieldset>

我想做一些类似于Name属性的事情.

解决方法

DisplayName属性(来自System.ComponentModel)执行类似的功能,可以应用于类.

MSDN

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读