I am trying to use array to loop through model fields
@Html.DisplayNameFor(model => model.[fld])
where fld is in an array
flds see code below. Please let me know how to do this.
@model IEnumerable<SchoolAutomationSite.Models.tblClass>
@{
ViewBag.Title = "List Of Classes";
}
<h2>@ViewBag.Title</h2>
<p>
@Html.ActionLink("Create New Class", "Create")
</p>
<table class="table">
<tr>
This is the Array
@{
string[] DisplayName = { "name", "description", "createdAt", "updatedAt" };
}
@foreach (var fld in flds)
{
<th>
@Html.DisplayNameFor(model => model.[fld])
How to do the above line?
</th>
}
</tr>
@foreach (var item in Model) {
<tr>
@foreach (var fld in flds)
{
<td>
@Html.DisplayFor(modelItem => item.[flds])
</td>
}
use the above instead off
----------
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.description)
</td>
<td>
@Html.DisplayFor(modelItem => item.createdAt)
</td>
<td>
@Html.DisplayFor(modelItem => item.updatedAt)
</td>
----------
</tr>
}
</table>