I have my View bound to IEnumerable<MyViewModel>.
MyViewModel class is annotated with a DisplayName like this:
[DisplayName("My other Display Name")]
public class MyViewModel
{
...
}
In my views which are directly bound to "MyViewModel" I used Html.DisplayNameForModel() and it works.
For the case of IEnumerable... I think Html.DisplayNameForInnerType... could be used but unfortunately I cannot help myself to find the expression required here.
Can anyone help with an example of how to use Html.DisplayNameForInnerType?
UPDATE1:
To answer a question in the comments: It is about ASP.Net Core 1.1

However, I coulnd't find it in the MS documentation.
Let's say that you have the following model:
public class MyViewModel
{
[Display(Name = "My Id")]
public int Id { get; set; }
[Display(Name = "My name")]
public string Name { get; set; }
}
In your view you can use Html.DisplayNameForInnerType like so:
@model IEnumerable<MyViewModel>
<table>
<thead>
<tr>
<th>@Html.DisplayNameForInnerType((MyViewModel item) => item.Id)</th>
<th>@Html.DisplayNameForInnerType((MyViewModel item) => item.Name)</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.Id</td>
<td>@item.Name</td>
</tr>
}
</tbody>
</table>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With