8

正如我们可以用来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财产的事情。

4

3 回答 3

6

DisplayName属性 (from ) 执行类似的System.ComponentModel功能并且可以应用于类。

MSDN

于 2012-07-02T21:08:08.317 回答
1

我真的不知道是否有另一种方法可以做到这一点,但我通常不hard code这样做,我使用在视图中创建一个变量,然后我在需要的地方调用。在你的情况下,我会做的更优雅一点

@{
    var viewName = typeof(Foo).Name;
}

@model Models.Person
<fieldset>
<legend>@viewName</legend>
<div class="display-label">
    @Html.LabelFor(model => model.Name)
</div>
</fieldset>
于 2012-07-02T21:25:51.997 回答
1

使用装饰器模式,只需将 DisplayAttribute 包装为您自己的自定义属性,专门用于类。

using System;
using System.ComponentModel.DataAnnotations;

namespace YourNameSpaceHere.Support
{
    [AttributeUsage(AttributeTargets.Class)]
    public class DisplayForClassAttribute : Attribute
    {
        protected readonly DisplayAttribute Attribute;

        public DisplayForClassAttribute()
        {
            this.Attribute = new DisplayAttribute();
        }

        public string ShortName
        {
            get { return this.Attribute.ShortName; }
            set { this.Attribute.ShortName = value; }
        }

        public string Name
        {
            get { return this.Attribute.Name; }
            set { this.Attribute.Name = value; }
        }

        public string Description
        {
            get { return this.Attribute.Description; }
            set { this.Attribute.Description = value; }
        }

        public string Prompt
        {
            get { return this.Attribute.Prompt; }
            set { this.Attribute.Prompt = value; }
        }

        public string GroupName
        {
            get { return this.Attribute.GroupName; }
            set { this.Attribute.GroupName = value; }
        }

        public Type ResourceType
        {
            get { return this.Attribute.ResourceType; }
            set { this.Attribute.ResourceType = value; }
        }

        public bool AutoGenerateField
        {
            get { return this.Attribute.AutoGenerateField; }
            set { this.Attribute.AutoGenerateField = value; }
        }

        public bool AutoGenerateFilter
        {
            get { return this.Attribute.AutoGenerateFilter; }
            set { this.Attribute.AutoGenerateFilter = value; }
        }

        public int Order
        {
            get { return this.Attribute.Order; }
            set { this.Attribute.Order = value; }
        }

        public string GetShortName()
        {
            return this.Attribute.GetShortName();
        }

        public string GetName()
        {
            return this.Attribute.GetName();
        }

        public string GetDescription()
        {
            return this.Attribute.GetDescription();
        }

        public string GetPrompt()
        {
            return this.Attribute.GetPrompt();
        }

        public string GetGroupName()
        {
            return this.Attribute.GetGroupName();
        }

        public bool? GetAutoGenerateField()
        {
            return this.Attribute.GetAutoGenerateField();
        }

        public bool? GetAutoGenerateFilter()
        {
            return this.Attribute.GetAutoGenerateFilter();
        }

        public int? GetOrder()
        {
            return this.Attribute.GetOrder();
         }  
     }
 }

用法如下:

[DisplayForClass(Name = "Approval Matrix")]
public class ApprovalMatrixViewModel
{
}
于 2017-03-10T15:43:20.240 回答