1

I am using property descriptor to generically iterate over the properties and print their attribute names as table header. It works fine printing the property name, but not for the [Display(Name = "Test")]

Class:

public class Transaction
{
    [Display(Name = "Test")]
    public string DELETE { get; set; }
    public string PARTNO { get; set; } //Part Number 

Markup:

    @foreach (System.ComponentModel.PropertyDescriptor descriptor in System.ComponentModel.TypeDescriptor.GetProperties(Model[0]))
    {
       <th onclick="tableColumnSort(this)" class="tableSortHeaderBtnStyle">@descriptor.DisplayName;</th>
    }

Just prints the "DELETE" name rather than "Test". The descriptor has a Name, and DisplayName option and both are set to "DELETE"

What am I doing wrong?

1
  • Is there descriptor.Identifier.Text property on your object? Commented Mar 31, 2017 at 19:33

2 Answers 2

1

You have to use [DisplayName("Test")] instead of [Display(Name = "Test")], please check this:

public class Transaction
{
    //[Display(Name = "Test")]
    [DisplayName("Test")]
    public string DELETE { get; set; }
    public string PARTNO { get; set; } //Part Number 
}

You can check this in DotNetFiddle.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you that worked. The other one is part of DataAnnotations.
1

Use [DisplayName("Test")] in place of [Display(Name = "Test")]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.