Paul
Paul

Reputation: 5576

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; }
    public string HelpText { get; set; }

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

Upvotes: 3

Views: 1773

Answers (1)

Rup
Rup

Reputation: 34408

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.

Upvotes: 1

Related Questions