简体   繁体   中英

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; }
}

I don't think so, but you can derive from DisplayNameAttribute and override DisplayNameValue { get; } 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM