因为我们可以使用 System.ComponentModel.DataAnnotations.DisplayAttribute
为属性设置标签,所以我想将它用于类,但不允许在类中使用。
using System.ComponentModel.DataAnnotations;
[Display(Name = "A person")]
public class Person
{
[Display(Name = "A name")]
public string Name { get; set; }
}
有人知道解决方法吗?
编辑: 我想在强类型 View 上使用它。当我创建一个新的强类型 View 时,类名是硬编码在 HTML 中的,如下所示:
@model Models.Person
<fieldset>
<legend>Person</legend>
<div class="display-label">
@Html.LabelFor(model => model.Name)
</div>
</fieldset>
我想做一些类似于 Name
属性的事情。
最佳答案
DisplayName
属性(来自 System.ComponentModel
)执行类似的功能,可以应用于类。
关于c# - 在类上使用 DisplayAttribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11301107/