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 Views Displaying a List of Values

I don't know why my code isn't working.

I thought for sure I am following the challenge instructions and the example from the previous video, but I'm still getting an error message. My code is below:

@{ Layout = null;

var title = "Super Mario 64";
var description = "Super Mario 64 is a 1996 platform video game developed and published by Nintendo for the Nintendo 64.";
var characters = new string[]
{
    "Mario",
    "Princess Peach",
    "Bowser",
    "Toad",
    "Yoshi"
};

}

<!DOCTYPE html>

<html> <head> <meta name="viewport" content="width=device-width" /> <title>Video Game Detail</title> </head> <body> <div> <h1>@title</h1>

    <h5>Description:</h5>
    <div>@description</div>

    <h5>Characters:</h5>
    <div>
        <ul>
            @foreach (var character in characters)
              {
                <li>@characters</li>;
              }
        </ul>
    </div>
</div>

</body> </html>

What am I doing wrong? I don't see it.

Detail.cshtml
@{
    Layout = null;

    var title = "Super Mario 64";
    var description = "Super Mario 64 is a 1996 platform video game developed and published by Nintendo for the Nintendo 64.";
    var characters = new string[]
    {
        "Mario",
        "Princess Peach",
        "Bowser",
        "Toad",
        "Yoshi"
    };
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Video Game Detail</title>
</head>
<body>
    <div>
        <h1>@title</h1>

        <h5>Description:</h5>
        <div>@description</div>

        <h5>Characters:</h5>
        <div>
            <ul>
                @foreach (var character in characters)
                  {
                    <li>@characters</li>;
                  }
            </ul>
        </div>
    </div>
</body>
</html>

2 Answers

andren
andren
28,558 Points

Your code is quite close, but there are two issues:

  1. You have typed @characters (the list) instead of @character (the value) inside the li
  2. You include a semicolon after the closing li tag, that causes a semicolon to actually be inserted in the HTML document which is incorrect.

If you fix the name and remove the semicolon like this:

@foreach (var character in characters)
{
    <li>@character</li>
}

Then your code will pass.

I made the changes you suggested, but still get an error message:

"Did you write the 'characters' string array values to the view using a 'foreach' loop?"

I'm not sure why I'm getting that message

Ben Reynolds
Ben Reynolds
35,170 Points
  1. On the line with the list item, you don't need a semicolon.
  2. You used "@characters" (plural, as in the list itself) instead of "@character", which will give you the character name for that loop iteration.