0

e.g. i have a class

public class Car
 {
     public string Name { get; set; }
     public string Brand { get; set; }
     public string Color { get; set; }
 }

and i have a view with a model as a list

@model IEnumerable<Car>

Now i want to display the raw displayName of a property of a listItem (model).

Example code:

@model IEnumerable<Car>

//(..)

<thead>
    <tr>
        <th>"[@Html.DisplayName(Model.First().Name))]"</th>
        <th>Brand</th>
        <th>Color</th>
    </tr>
</thead>

How can I do it?

UPDATE

I found the solution in creating a new helper class: click me

sandy
  • 464
  • 1
  • 6
  • 13
  • @PeterB with "Model.First()" I can access the property values. But i actually don't want to display the values but the displayname of a property. – sandy Dec 05 '18 at 12:11
  • 1
    Possible duplicate of [Get \[DisplayName\] attribute of a property in strongly-typed way](https://stackoverflow.com/questions/5474460/get-displayname-attribute-of-a-property-in-strongly-typed-way) – Peter B Dec 05 '18 at 12:12

2 Answers2

0

Use LabelFor

<th>@Html.LabelFor(p => p.First().Name)<th>

Don't forget the display Attribute on Car Class

[DisplayName("Car Name")]
public string Name { get; set; }
Ivan Valadares
  • 869
  • 1
  • 8
  • 18
  • with LabelFor I'll get the displayname i want in a "label", but i want to have the raw value: `Name` and not `` – sandy Dec 05 '18 at 12:32
  • Ok, you can get it by reflection using System.Reflection; var name = ((DisplayAttribute)(typeof(Car).GetProperty("Name").GetCustomAttribute(typeof(DisplayAttribute)))).Name; – Ivan Valadares Dec 05 '18 at 12:51
0

You can get

<th>DisplayName</th>

by using

@Html.DisplayNameFor(model => model.First().Name)

The solution you've used is quite old and doesn't use the built in helper.

melkisadek
  • 1,043
  • 1
  • 14
  • 33