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

Problems with foreach in code challenge

I have the following code when trying to solve a code challenge:

@model Treehouse.Models.VideoGame[]

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

<h1>@ViewBag.PageTitle</h1>

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

The compiler doesn't give any errormessages, but I get a notification saying Bummer: Did you use 'foreach' to ...

I'm not sure how to solve this, I've tried multiple ways to generate the foreach-loop, and this should be the correct syntax, but I keep getting the same error.

Could someone please review the code and if they see the error please let me know?

Thanks in advance!

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

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

<h1>@ViewBag.PageTitle</h1>

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

1 Answer

Instead of having one <li> element, try moving the <li> and </li> tags to within the foreach loop so that each value of videoGame gets its own <li> element.

Since DisplayText is a property rather than a function, change @videoGame.DisplayText(); to @videoGame.DisplayText

That fixes it! Thanks!