1

Here is what I have been toying with.

I have a class like this;

public partial class DespatchRoster : DespatchRosterCompare, IGuidedNav
{
    public string despatchDay { get; set; }
}

I've added meta data to it.

[MetadataType(typeof(RosterMetadata))]
public partial class DespatchRoster
{
}

public class RosterMetadata
{
    [Display(Name="Slappy")]
    public string despatchDay { get; set; }
}    

In my HTML I have the following;

<% PropertyInfo[] currentFields = typeof(DespatchRoster).GetProperties(); %>

<% foreach (PropertyInfo propertyInfo in currentFields){ %>
  <li class="<%= propertyInfo.Name %>"><%= propertyInfo.Name %></li>
<%} %>

What I want to see is Slappy as an LI instead of despatchDay.

I know i've done this before but can't think how.

tereško
  • 58,060
  • 25
  • 98
  • 150
griegs
  • 22,624
  • 33
  • 128
  • 205

3 Answers3

1

Try to use the one below as mentioned by this.

    private string GetMetaDisplayName(PropertyInfo property)
    {
        var atts = property.DeclaringType.GetCustomAttributes(
            typeof(MetadataTypeAttribute), true);
        if (atts.Length == 0)
            return null;

        var metaAttr = atts[0] as MetadataTypeAttribute;
        var metaProperty =
            metaAttr.MetadataClassType.GetProperty(property.Name);
        if (metaProperty == null)
            return null;
        return GetAttributeDisplayName(metaProperty);
    }

    private string GetAttributeDisplayName(PropertyInfo property)
    {
        var atts = property.GetCustomAttributes(
            typeof(DisplayNameAttribute), true);
        if (atts.Length == 0)
            return null;
        return (atts[0] as DisplayNameAttribute).DisplayName;
    }
aiapatag
  • 3,355
  • 20
  • 24
0

Try this:

var properties = typeof(DespatchRoster ).GetProperties()
    .Where(p => p.IsDefined(typeof(DisplayAttribute), false))
    .Select(p => new
        {
          PropertyName = p.Name, p.GetCustomAttributes(typeof(DisplayAttribute),false)
                          .Cast<DisplayAttribute>().Single().Name
        });
Bhushan Firake
  • 9,338
  • 5
  • 44
  • 79
0

Give this a try:

Since you are accessing the metadata outside of "normal" MVC validation or display templates, you will need to register the TypeDescription yourself.

[MetadataType(typeof(RosterMetadata))]
public partial class DespatchRoster
{
    static DespatchRoster() {
        TypeDescriptor.AddProviderTransparent(
            new AssociatedMetadataTypeTypeDescriptionProvider(typeof(DespatchRoster), typeof(RosterMetadata)), typeof(DespatchRoster));
    }
}

public class RosterMetadata
{
    [Display(Name="Slappy")]
    public string despatchDay { get; set; }
}

Then to access the Display Name we need to enumerate the properties using the TypeDescriptor not the normal PropertyInfo method.

<% PropertyDescriptorCollection currentFields = TypeDescriptor.GetProperties(typeof(DespatchRoster)); %>

<% foreach (PropertyDescriptor pd in currentFields){ %>
  <% string name = pd.Attributes.OfType<DisplayAttribute>().Select(da => da.Name).FirstOrDefault(); %>
  <li class="<%= name %>"><%= name %></li>
<%} %>
Josh
  • 61
  • 4