c#中BE属性的标签

本文关键字:标签 属性 BE | 更新日期: 2023-09-27 17:51:08

在我的BE类中,我有某些属性与表字段匹配。我想公开这些属性的描述性名称。例如,将其显示为网格中的列标头。

例如,有一个属性叫做FirstName。我想公开它的描述性名称First Name

为此,我创建了一个pair数组作为这个BE类的属性。也就是说,myarray("FirstName","First Name")有更好的方法吗?

c#中BE属性的标签

您可以在您的模型中这样做:

[Display(Name = "First Name")]
public string FirstName { get; set; }

然后在视图中你可以像这样引用标签名:

@Html.DisplayFor(m=>m.FirstName)

您可以在BE属性上使用[DisplayName("First name")]属性。

然后在视图中使用:@Html.LabelFor(m=>m.FirstName)

类似的问题在这里的SO:如何改变LabelFor在剃须刀在mvc3的显示名称?

编辑

您还可以在所有BE属性上使用[Display(Name="First name")]属性。然后创建一个模板来显示你的BE(更多信息如何创建模板在这里:我如何创建一个MVC Razor模板DisplayFor())。

然后在视图中使用:

@Html.DisplayFor(m=>m, "MyModelTemplateName")

我发现这很有用,这就是我解决它的方法。我发这篇文章是因为它可能对其他人有用。

在BE中定义这个

[DisplayName("First Name"), Description("First Name of the Member")]
public string FirstName
{
    get { return _firstName; }
    set { _firstName = value; }
}

您可以在下面阅读每个属性的详细信息;

PropertyDescriptorCollection propertiesCol = TypeDescriptor.GetProperties(objectBE);
PropertyDescriptor property;
for (int i = 0; i < propertiesCol.Count; i++)
{
    property = TypeDescriptor.GetProperties(objectBE)[i];
    /*
    // Access the Property Name, Display Name and Description as follows
    property.Name          // Returns "FirstName"
    property.DisplayName   // Returns "First Name"
    property.Description   // Returns "First Name of the Member"
    */
}
  • 其中objectBE为BE类的对象实例。