Hi,
So I've eagerly added the System.ComponentModel.DataAnnotations namespace to my model.
I've added things such as:
[Required] [DisplayName("First Name")]
public string first_name {get;set;}
I really like these attributes because they save me from having to write custom T4 and/or modify views heavily. This way, I can regenerate a view confident that it will add the display names I want, etc.
The problem comes in when I started building a DataGrid helper inspired by the one in ASP.NET MVC2 unleashed.
In this helper, Stephen uses reflection to get at the column headings.
var value=typeOf(T).GetProperty(columnName).GetValue(item,null) ?? String.Empty;
Well, the trouble is I don't want to retrieve the property name. I want to retrieve the value for the DisplayName attribute.
My first attempt at this was to look inside the Attributes property of the PropertyInfo class. Unfortunately, none of the data annotations show up as an attribute.
Is there a way to retrieve the data annotations using reflection?
Thanks,
Ron