开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜