How to get the Value in [Display(Name=“”)] attribute in Controller for any property using EF6

前端 未结 3 1910
北荒
北荒 2021-01-04 04:19

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.

相关标签:
3条回答
  • 2021-01-04 05:00

    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.

    0 讨论(0)
  • 2021-01-04 05:03

    This should work:

    MemberInfo property = typeof(ABC).GetProperty(s); 
    var dd = property.GetCustomAttribute(typeof(DisplayAttribute)) as DisplayAttribute;
    if(dd != null)
    {
      var name = dd.Name;
    }
    
    0 讨论(0)
  • 2021-01-04 05:06

    You can use it:

    MemberInfo property = typeof(ABC).GetProperty(s); 
    var name = property.GetCustomAttribute<DisplayAttribute>()?.Name;
    
    0 讨论(0)
提交回复
热议问题