开发者

Set display name to value of another property - MVC3 Data Annotations

开发者 https://www.devze.com 2023-02-16 12:27 出处:网络
Is it possible to set the DisplayName Data Attribute to the value of another property in a viewmodel?

Is it possible to set the DisplayName Data Attribute to the value of another property in a viewmodel?

public class FieldModel
{
    public string DisplayText { get; set; }
    public bool Mandatory { get; set; }
    public string DataType { get; set; }开发者_如何学Go
    public string HelpText { get; set; }

    [DisplayName(this.DisplayText)]
    public string Value { get; set; }
}


I don't think so, but you can derive from DisplayNameAttribute and override DisplayNameValue { get; } to fetch the value from your class instead.

(apologies, don't have time to write a code sample just now)

It looks like MVC will use DisplayAttribute.GetName() in preference to DisplayNameAttribute but that's a sealed class.

0

精彩评论

暂无评论...
验证码 换一张
取 消