TOC

The community is working on translating this tutorial into Swedish, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

Razor:

Loops

Looping is an extremely useful programming technique which you can also benefit a lot from in your Razor code. Looping allows you to repeat an action and/or output for a specific amount of iterations - for instance to output items in a list, as we'll see in the examples of this article.

There are several types of loops in C# and they are all available in your Razor code. Let's go through all of them, using the same, very simple data source: a list of names, which we'll turn into an unordered HTML list.

The data source

Start by defining a list of names in the top of your View file like this:

@{
List<string> names = new List<string>()
{
"John Doe",
"Jane Doe",
"Joe Doe",
"Jenna Doe",
"Doggy Doe"
};
}

You're now ready to test the various types of loops.

The "for" loop

The for loop is especially well suited in situations where you need to keep track of how far in the looping process you are, because you can always access the counting variable (called i in this example):

<ul>
@for (int i = 0; i < names.Count; i++)
{
<li>@names[i]</li>
}
</ul>

The "foreach" loop

The foreach loop is definitely the easiest to use for a task like this:

<ul>
@foreach (string name in names)
{
<li>@name</li>
}
</ul>

The "while" loop

The while loop is better suited for other use-cases than this one, but you can still use it:

<ul>
@{
int counter = 0;
}
@while(counter < names.Count)
{
<li>@names[counter++]</li>
}
</ul>

The "do...while" loop

Much like the while loop, this one is more relevant for other types of looping tasks. The difference between the while and the do...while loops is when the condition is evaluated. For the while loop, it's evaluated before entering the first iteration, meaning that it might never loop - the condition of a do...while loop is evaluated after the first iteration, meaning that it will always loop at least once:

<ul>
@{
counter = 0;
}
@do
{
<li>@names[counter++]</li>
} while (counter < names.Count);
</ul>

break/continue

Common for all these loops is of course the ability to break (completely leave the loop) and continue (stop the current iteration and jump to the next). Here's a revised example of the for loop where we use an if statement to make sure that we stop the loop after we have printed a maximum of 3 names:

<ul>  
@for (int i = 0; i < names.Count; i++)  
{  
<li>@names[i]</li>  
@if(i >= 2)  
{  
<li>...and so on</li>  
break;  
}  
}  
</ul>

Summary

As you can see, looping over data with Razor code is just like doing it in C#. You can even jump in and out of markup, as illustrated by the examples. This article serves mostly as an illustration of how easy it is to loop over data with Razor - if you want to know more about the theory behind each loop, please consult our C# tutorial.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!