Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor Html.DisplayNameForInnerType for IEnumerable ViewModel

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 Html.DisplayNameForInnerType<>

However, I coulnd't find it in the MS documentation.

like image 352
mJay Avatar asked Feb 17 '26 18:02

mJay


1 Answers

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>
like image 51
Charlie Avatar answered Feb 20 '26 10:02

Charlie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!