Scaffolded Views in MVC5 w/ EF6 produce Code like this for an Overview page:
@*Useroverview.cshtml*@
@model IEnumerable<Models.tblUser>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@*One of the lines in question*@
@Html.DisplayNameFor(model => model.username)
</th>
<th>
@*Another line in question*@
@Html.DisplayNameFor(model => model.email)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
@*looping through the elements and displaying the values*@
}
</table>
How is it possible that the line I marked works? When I try that with proper ViewModels it does not allow me to do something exactly like it (i.e. access a Class' Member even though I'm accessing it through a List, but not through an element.
@Html.DisplayNameFor(model => model.username)
If my model is
@model IEnumerable<Models.MyOwnModel>
and contains, among other things, an IEnumerable users, I cannot access the property name with:
@Html.DisplayNameFor(model => model.users.username)
but only like so:
@Html.DisplayNameFor(model => model.users.ElementAt(0).username)
Edit: In the end, the result should be a line that writes out the variable's name (or whatever has been set as the display name in the model class).