Asp.net mvc 3 MVC3属性的不同显示名称

Asp.net mvc 3 MVC3属性的不同显示名称,asp.net-mvc-3,data-annotations,Asp.net Mvc 3,Data Annotations,我的视图模型上有以下属性: [Display(Name ="Contractor Name")] public string Name { get; set; } 由于我在不同的地方使用这个视图模型,我想根据派生类将属性的名称更改为“Customer name”、“Some other name”等。这将帮助我获得正确的验证消息等 是否可以在派生类中重写Name属性 谢谢这对我很有效 public class PropertyTitle : DisplayNameAttribute {

我的视图模型上有以下属性:

[Display(Name ="Contractor Name")]
public string Name { get; set; }
由于我在不同的地方使用这个视图模型,我想根据派生类将属性的名称更改为“Customer name”、“Some other name”等。这将帮助我获得正确的验证消息等

是否可以在派生类中重写Name属性

谢谢

这对我很有效

public class PropertyTitle : DisplayNameAttribute
{
    public int _ID { get; set; }

    public PropertyTitle(int ID)
    {
        this._ID = ID;
    }

    public override string DisplayName
    {
        get
        {
            if(_ID == 1)
               return "1";
            else if(_ID == 1)
               return "2";
            return "";
        }
    }
}

public class TestModel
{
    [PropertyTitle(2)]
    public string MyTextField { get; set; }
}

马丁的评论达到了目的


是的,这是可能的。我已经做到了。在派生类中,只需重写 名称属性,并为其指定另一个注释属性马丁 史密斯


谢谢

是的,这是可能的。我已经做到了。在派生类中,只需重写
Name
属性并为其提供另一个注释属性。哦,是的!这比拥有另一个假财产要好。多谢!请加上这个作为答案?有什么意义?如果要有两个不同的
propertytile
注释,为什么不只使用两个不同的
DisplayName
注释并完成它呢?