I'm surprised why the following table finds the names correctly.
@model IEnumerable<Member>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<tr>
...
The model I've set is an array of type Member so the model has no property LastName. An element in the list does! Shouldn't it be
<th>
@Html.DisplayNameFor(model => model.First().LastName)
</th>
I never noticed that until I changed my viewmodel to
public class IndexVm
{
public IEnumerable<Member> Members { get; set; }
}
and tried to
<th>
@Html.DisplayNameFor(model => model.Members.LastName)
</th>
This stopped working and when I investigated, I realized that I can see why. I can't understand how it could work previously, though...
- Can someone explain what's up with that?
- Is the below the only option given my new viewmodel?
How is the computer supposed to know what to display if the list is empty?
@Html.DisplayNameFor(model => model.Members.FirstOrDefault()?.LastName)
So confused...