griegs
griegs

Reputation: 22760

Get Display attribute of class when displaying in HTML page

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.

Upvotes: 1

Views: 864

Answers (3)

Josh
Josh

Reputation: 61

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

Upvotes: 0

aiapatag
aiapatag

Reputation: 3430

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

Upvotes: 1

Bhushan Firake
Bhushan Firake

Reputation: 9448

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

Upvotes: 0

Related Questions