Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Jon Andreas Grenness
Jon Andreas Grenness
2,686 Points

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