2

I don't quite understand the following strongly typed models syntax:

@model IEnumerable<MvcMovie.Models.Movie>

@{
    ViewData["Title"] = "Index";
}

<h2>Index</h2>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Genre)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Price)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ReleaseDate)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Title)
            </th>
            <th></th>
        </tr>
    </thead>

Why do we access the Genre property like this?

@Html.DisplayNameFor(model => model.Genre)

I thought that the model here refers to the whole container:

@model IEnumerable<MvcMovie.Models.Movie>

Am I wrong?

FrozenHeart
  • 19,844
  • 33
  • 126
  • 242
  • 1
    I believe [this SO post](http://stackoverflow.com/questions/39029941/asp-net-mvc-how-use-displaynamefor-in-order-to-create-a-table-heading-and-body) will answer your question. – Michael Oct 13 '16 at 09:08

1 Answers1

-1

Did you try to execute the code you have shown? I don't think it will work. It looks like the tutorial you have linked shows you a div (using yellow highlights) to clear up the difference between @model MvcMovie.Models.Movie and @model IEnumerable<MvcMovie.Models.Movie>.

So the correct way would be:

@model IEnumerable<MvcMovie.Models.Movie>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Genre)
            </td>
         @* ... *@
        </tr>
}
Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36