Razor supports VB (Visual Basic) in ASP.NET. This section describes how to easily execute the same statement repeatedly.
Statements are executed repeatedly in the loop.
If you need to execute the same statement repeatedly, you can set up a loop.
If you know the number of times you want to loop, you can use a for loop . This type of loop is especially useful when counting up or down:
<html> <body> @For i=10 To 21@<p>Line #@i</p>Next i </body> </html>
If you are working with collections or arrays, you will often use the for each loop .
A collection is a group of similar objects, and a for each loop can iterate through the collection until it is complete.
In the following example, the ASP.NET Request.ServerVariables collection is traversed.
<html> <body> <ul> @For Each x In Request.ServerVariables@<li>@x</li>Next x </ul> </body> </html>
The while loop is a general loop.
A while loop begins with the while keyword, followed by parentheses where you specify how long the loop will last, and then a block of code that is repeated.
A while loop usually sets an incrementing or decrementing variable for counting.
In the following example, the += operator adds 1 to the value of variable i each time the loop is executed.
<html> <body> @CodeDim i=0Do While i<5i += 1@<p>Line #@i</p>LoopEnd Code </body> </html>
When you want to store multiple similar variables but don't want to create a separate variable for each variable, you can use an array to store:
@CodeDim members As String()={"Jani","Hege","Kai","Jim"}i=Array.IndexOf(members,"Kai")+1len=members.Lengthx=members(2-1) endCode <html> <body> <h3>Members</h3> @For Each person In members@<p>@person</p>Next person <p>The number of names in Members are @len </p> <p>The person at position 2 is @x </p> <p>Kai is now in position @i </p> </body> </html>