FAQ: Razor Pages Syntax I - Working with Loops

This community-built FAQ covers the “Working with Loops” exercise from the lesson “Razor Pages Syntax I”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Build Web Apps with ASP.NET

FAQs on the exercise Working with Loops

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

My problems with this exercise are twofold:

  1. This exercise has what I believe to be a bug in it. The second task is either unclear, or the verification is incorrectly marking solutions as invalid. The task asks that I add a pair of unordered list tags under the <h5> heading (which is, in actuality an <h4> tag, but a minor typo isn’t why I’m here). When I add the unordered list tags and click run to move to the next task, the task comes up with a failure and asks if I put the <ul></ul> tags in. I have tried multiple combinations of solutions and have yet to find a way to make it work. In the end I asked for the solution, copied it, reset the exercise, and pasted the exact code. The second task still fails.

  2. Even when I ask for the solution for the exercise, the results are not correctly rendered in the webpage view sidebar.

Not sure what to do aside from take the solution and move on, but the completionist in me hates to do it.

I’m having the exact same problem. I’ve actually been having this issue with the entire ASP.NET course, what I think is happening is the test that controls progression through the lesson is very strict and is looking for an exact character match since there doesn’t seem to be a good way to assess the results. I’ve found multiple situations where the code works just fine, it’s doing what the lesson wants it to do, and it still won’t recognize it. I’ve had some very nitpicky issues like having a space between a for and the parentheses, or not having a space between the closing parentheses and the opening bracket. Unfortunately I just don’t think that this platform is the best for learning ASP.NET, although I’ve been pretty satisfied with the base C#, Python, and computer science modules.

I just caught the problem I was having on the last evaluation. I had one code block for the entire while loop structure, but the solution expected two different code blocks. Give me a break.

I’m unsure of why I am failing step 5 here. This git setup is really nice and helpful. Best update in years.

@model IndexModel

<!-- Create favoriteFoods below
Create a list of 4 of your favorite foods and assign them to a variable called favoriteFoods.

Open some tags for an unordered list under the <h4> heading with the text: “Results from your for loop”.

Inside the unordered list, you’ll be looping over 4 of your favorite foods and display them as list items.
Start by creating a for loop with the following conditions:

An initial variable, i, with a count starting at 0
A terminating condition that ends when we reach the count of our favoriteFoods length.
An iteration statement that increases our initial value by 1.
After the for loop expression, open some curly brackets where you’ll write the rest of your C# code.

Within your code blocks, open a list item tag.
Inside it, access each element in the favoriteFoods list using the initial variable, i.

Now you have your favoriteFoods displayed!
Let’s rewrite this loop in a different way.
Instead of creating a for loop,
open a pair of <ul> tags and use a foreach loop and call each item on the list.

	List<string> favoriteFoods = new List<string>()

<div class="text-center">
	<h1 class="display-5">Let's work with loops!</h1>

	<h4>Results from your for loop:</h4>
		@for (int i = 0; i < favoriteFoods.Count; i++)
	<h4>Results from your foreach loop:</h4>
		@foreach (string item in @favoriteFoods)
	<h4>Results from your while loop:</h4>

I’m a bit late to the party but (as stupid as this is) your @foreach you can’t have a space between @foreach(string…)

No, that’s not true at all in any case with expressions you use.


Even MS does it…

@page "/todo"


    @foreach (var todo in todos)

@code {
    private List<TodoItem> todos = new();

If you are saying I am failing because of the space then that’s an error on CA, not me.

Sorry yes, I’m saying it’s a failing on CA, I had the same issue and it only passed when I removed the whitespace.

1 Like