I have a nested class such as :
public class Jar
{
public IEnumerable<NailClippings> Nails { set; get; }
public IEnumerable<People> Donors { set; get; }
}
My controller passes a single Jar to my view, string typed as
@model Test.ViewModels.Jar
I can loop through the contents of Nails and Donors easily with something like this
@foreach(var item in Model.Nails)
My issue is with using HTML helpers to generate Display names for me
@Html.DisplayNameFor(model => model.Nails.Length)
I have come up with a solution by changing the type of the inner nested classes to List, appending .ToList() at the end of my queries and changing the line to
@Html.DisplayNameFor(model => model.Nails[0].Length)
Being forced to use List and have an index [0] to display a name is goofy to me. Is there an alternative method for referencing inner nested class attributes?
Edit: View
<table>
<tr>
<th>@Html.DisplayNameFor(model => model.Nails[0].Length)</th>
.
.
</tr>
@foreach(var item in Model.Nails){
<tr>
<td>@Html.DisplayFor(modelItem => item.Length</td>
.
.
</tr>
}
</table>
Instead of
You could use
Without having to convert to a
List.