Loop through Model items in ASP.NET MVC
To loop through Model items in ASP.NET MVC view, use foreach loop in the Controller,which returns Collection of items.
Add the given below line to bind your model with .cshtml page.
- 1: @model IEnumerable<BookStore.Models.Category>
Now write foreach loop at view level page(.cshtml).
- @if (Model != null)
- {
- foreach (var item in Model)
- {
- <ul id="menu">
-
- <li>
- @Html.DisplayFor(modelItem => item.Address)
- </li>
- <li>
- @Html.DisplayFor(modelItem => item.City)
- <br />
- </li>
-
- <li>
- @Html.DisplayFor(modelItem => item.Id)
- </li>
- </ul>
- }
- }