17

I am developing an MVC 5 application. I want to get the value in [Display(Name = "")] attribute in my controller method for any property of any class.

My model is as:

public partial class ABC
{
   [Required]
   [Display(Name = "Transaction No")]
   public string S1 { get; set; }
}

I have looked answer to this question, but it is a little lengthy procedure. I am looking for something readily available and built-in.

So, I have tried this:

MemberInfo property = typeof(ABC).GetProperty(s); // s is a string type which has the property name ... in this case it is S1
var dd = property.CustomAttributes.Select(x => x.NamedArguments.Select(y => y.TypedValue.Value)).OfType<System.ComponentModel.DataAnnotations.DisplayAttribute>();

But I have 2 problems, First I am not getting the value i.e. "Transaction No". And secondly even though I have mentioned .OfType<> I am still getting all attributes i.e. [Display(Name="")] and [Required].

But luckily I am getting the "Transaction No" value in

property>>CustomAttribute>>[1]>>NamedArguments>>[0]>>TypedValue>>Value = "Transaction No"

Since TypedValue.Value has the required value, So how can I retrieve it?

Community
  • 1
  • 1
Awais Mahmood
  • 1,308
  • 4
  • 21
  • 51

4 Answers4

28

This should work:

MemberInfo property = typeof(ABC).GetProperty(s); 
var dd = property.GetCustomAttribute(typeof(DisplayAttribute)) as DisplayAttribute;
if(dd != null)
{
  var name = dd.Name;
}
Alex Art.
  • 8,711
  • 3
  • 29
  • 47
  • THANKS.. It worked for me.. **typeof(ABC)**// or the model for which property is required – Awais Mahmood Sep 27 '15 at 13:22
  • I tried it but the value is not passed to property. I tried replacing s with my model's value. But when i hover at memberinfo property.... the property is null. why is this? I don't know if it is because my model value is enum? how can i fix it? this is my line: typeof(MyProject.Dom.Title).GetProperty(Card.Title.ToString()) – Nurul Oct 02 '17 at 08:06
  • 3
    @Nurul, Comment is not a place to ask questions like "here is my code, why it doesn't work". please post it as a separate question, and include your code. – Alex Art. Oct 03 '17 at 06:04
  • Why does this take so much code? Seems like a simple thing to do – Kellen Stuart Apr 12 '19 at 17:23
5

You can use it:

MemberInfo property = typeof(ABC).GetProperty(s); 
var name = property.GetCustomAttribute<DisplayAttribute>()?.Name;
Ahmed Galal
  • 694
  • 7
  • 21
3

This is Ahmed Galal's nice answer formulated as a static method in a utility class, for convenience:

using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace Project
{
    public static class AttributeGetter
    {
        public static string DisplayName<T>(string propertyName)
        {
            MemberInfo property = typeof(T).GetProperty(propertyName);
            return property.GetCustomAttribute<DisplayAttribute>()?.Name;
        }
    }
}

Usage:

string displayName = AttributeGetter.DisplayName<VM>(nameof(VM.Prop));
MarredCheese
  • 17,541
  • 8
  • 92
  • 91
2

Alex Art's answer almost worked for me. dd.Name simply returned the property name, but dd.GetName() returned the text from the Display attribute.

Bugs
  • 4,491
  • 9
  • 32
  • 41
Marc Levesque
  • 154
  • 1
  • 6