For Loop in Asp.Net MVC Razor View Engine Example

Generally, the loops in asp.net mvc razor view will work same as other programming languages. We can define loop inside or outside code block in the razor and use the same looping concept for assign value, define condition and increment, or decrement value. 

Syntax of For Loop in MVC Razor View

The following represents the syntax of for loop in asp.net mvc razor view engine.

 

@for (int i = 0; i < 5; i++)

 

{

 

//Code Block

 

}

Example of For Loop in MVC Razor View

The following is the example of for loop in the mvc razor view.

 

<h2>For loop</h2>

 

Output:-<br/>

 

@for (int i = 0; i < 5; i++)

{

 

@i<br/>

 

 

}

 

Output

The following is the output of for loop example in asp.net mvc razor view engine.

 

for loop in asp.net mvc razor view engine example