I have a list of "Book" (e.g.) as a model in view
@model List<Book>
I want to create a table that each column get it's header by Book's DisplayName propery:
<tr>
<th class="text-right">
@Html.DisplayNameFor(modelItem => Model.someMetaData.lineNumber)
</th>
<th class="text-right">
@Html.DisplayNameFor(modelItem => Model.someMetaData.FirstName)
</th>
<tr>
You can use DisplayNameFor
helper method when your model is of type IEnumerable<T>
@model IEnumerable<Book>
<table class="table table-responsive table-striped">
<tr>
<th>@Html.DisplayNameFor(a=>a.Id)</th>
<th>@Html.DisplayNameFor(a => a.Name)</th>
</tr>
@foreach (var b in Model)
{
<tr>
<td>@b.Id</td>
<td>@b.Name</td>
</tr>
}
</table>
This works only if your view is strongly typed to an IEnumerable<Book>
, It will not work for List<Book>