Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# ASP.NET MVC Basics Adding a List Page Creating a List View

Josh Boersma
PLUS
Josh Boersma
Courses Plus Student 2,486 Points

Using the Razor syntax, enumerate the view's Model property

Challenge Task 1 of 1

Using the Razor syntax, enumerate the view's Model property (containing an array of VideoGame object instances) within the provided <ul> element. Use a foreach loop to render each VideoGame object's DisplayText property value within its own <li> element. Name your loop value variable videoGame. Be sure to remove the placeholder <!-- video games --> HTML comment.

It asks me if I used the view's Model property as the source for the foreach loop. I did it exactly as in the video, and it compiled and worked in my code as I followed along. Any pointers?

Index.cshtml
@model Treehouse.Models.VideoGame[]

@{
    ViewBag.PageTitle = "Video Games";  
}

<h1>@ViewBag.PageTitle</h1>

<div>
    <ul>
      @foreach (var videoGame in Model.VideoGame)
      {
        <li>@videoGame.DisplayText</li>
      }
    </ul>
</div>

1 Answer

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hi Josh,

This one is a bit tricky because the Model property is set to the array itself at the top of the file. That means there's no VideoGame property. Model itself is the array.

@model Treehouse.Models.VideoGame[]
...

So you can loop through each VideoGame object like so.

...
<div>
    <ul>
      @foreach (var videoGame in Model)
      {
      <li>@videoGame.DisplayText</li>
      }
    </ul>
</div>

I hope this helps.