---
title: "How to use foreach loop in ASP.NET MVC .cshtml file?"  
description: "How to use foreach loop in ASP.NET MVC .cshtml file?"  
author: "Ashutosh Patel"  
published: 2022-08-19  
updated: 2025-02-10  
canonical: https://answers.mindstick.com/qa/98063/how-to-use-foreach-loop-in-asp-dot-net-mvc-cshtml-file  
category: "asp.net"  
tags: ["asp.net mvc", "c#"]  
reading_time: 1 minute  

---

# How to use foreach loop in ASP.NET MVC .cshtml file?

I want to show [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) [records](https://yourviews.mindstick.com/view/87521/olympic-highlights-2024-10-best-players-and-medal-records) in a list on the [page](https://www.mindstick.com/articles/85722/common-on-page-seo-mistake-how-to-fix) in MVC. I want to use [foreach loop](https://www.mindstick.com/forum/155785/how-to-use-foreach-loop-in-mvc-razor-view) for it, [anyone](https://www.mindstick.com/articles/23207/how-to-find-the-best-fit-job-for-anyone) can help me how to [apply](https://www.mindstick.com/articles/13148/discover-to-apply-make-up-like-a-professional-supermodel) it?

## Answers

### Answer by Steilla Mitchel

```cs
@foreach (var item in Model.Foos)
{
    <div>@item.Bar</div>
}
```

### Answer by Ravi Vishwakarma

In an ASP.NET MVC `.cshtml` file, you can use the `foreach` loop inside Razor syntax to iterate over a collection and generate dynamic HTML.

#### Example 1: Iterating Over a List in the View

If you pass a list from the controller to the view, you can loop through it in the `.cshtml` file.

**Controller (**`HomeController.cs`**)**

```cs
public ActionResult Index()
{
    List<string> names = new List<string> { "Alice", "Bob", "Charlie" };
    return View(names);
}
```

**View (**`Index.cshtml`**)**

```cs
@model List<string>

<ul>
    @foreach (var name in Model)
    {
        <li>@name</li>
    }
</ul>
```

## Output:

- Alice
- Bob
- Charlie


---

Original Source: https://answers.mindstick.com/qa/98063/how-to-use-foreach-loop-in-asp-dot-net-mvc-cshtml-file

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
