---
title: "How to use Foreach Loop in MVC Razor view?"  
description: "How to use Foreach Loop in MVC Razor view?"  
author: "Anonymous User"  
published: 2020-02-05  
updated: 2020-02-05  
canonical: https://www.mindstick.com/forum/155785/how-to-use-foreach-loop-in-mvc-razor-view  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 1 minute  

---

# How to use Foreach Loop in MVC Razor view?

[Briefly explain](https://www.mindstick.com/forum/156854/briefly-explain-the-concept-of-constructor-overloading) to me how [foreach loop](https://www.mindstick.com/forum/182/problem-in-getting-the-value-from-array-by-using-foreach-loop) will work in [asp.net MVC](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc) with an example.

## Replies

### Reply by Nishi Tiwari

The loops in [asp.net](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) [MVC](https://www.mindstick.com/forum/155803/define-cache-profile-in-mvc) razor view will also work the same as other programming languages. But in razor, we can define loop **[inside or outside code block](https://www.mindstick.com/forum/155784/how-to-use-for-loop-in-mvc-razor-view)** and we can use the same [foreach](https://www.mindstick.com/forum/33870/how-parallel-foreach-works-internally) looping concept to assign value, define the condition.

## The syntax of using foreach loop in asp.net MVC razor view is given below

```
foreach (var item in marks)
{

//Code Block

}

```

## The example of using foreach loop in asp.net MVC razor view is shown below

```
<h2>ForEach loop</h2>

Output:-<br/>

@{
var marks = Enumerable.Range(0,5);

foreach (var item in marks)
{
@item<br/>
}

}

```

## The output of Foreach Loop in MVC

The output of foreach loop example in asp.net MVC Razor view engine

```
01234
```


---

Original Source: https://www.mindstick.com/forum/155785/how-to-use-foreach-loop-in-mvc-razor-view

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
