Your view model is not adapted to what you are trying to achieve. Here's what the best review model would look like:
public class MyViewModel {
and then:
@model MyViewModel <table> <tr> <th> @Html.LabelFor(x => x.NameHeader) </th> </tr> @foreach (var item in Model.Users) { <tr> <td> @Html.DisplayFor(modelItem => item.Name) </td> </tr> </table>
and with the display template you donโt even need to write a foreach :
@model MyViewModel <table> <tr> <th> @Html.LabelFor(x => x.NameHeader) </th> </tr> @Html.DisplayFor(x => x.Users) </table>
and inside a custom display template ( ~/Views/Shared/DisplayTemplates/User.cshtml ):
@model User <tr> <td>@Html.DisplayFor(x => x.Name)</td> </tr>
Darin Dimitrov
source share