---
title: "How to create a Razor view that iterates over a collection of items and displays them dynamically."  
description: "How to create a Razor view that iterates over a collection of items and displays them dynamically."  
author: "Utpal Vishwas"  
published: 2023-10-20  
updated: 2023-10-23  
canonical: https://www.mindstick.com/forum/160239/how-to-create-a-razor-view-that-iterates-over-a-collection-of-items-and-displays-them-dynamically  
category: ".net core"  
tags: ["asp.net core", ".net core", "razor view"]  
reading_time: 2 minutes  

---

# How to create a Razor view that iterates over a collection of items and displays them dynamically.

How to create a [Razor view](https://www.mindstick.com/forum/155820/how-to-generate-textbox-control-using-htmlhelper-in-razor-view) that iterates over a [collection](https://www.mindstick.com/articles/1718/collections-in-java) of [items](https://www.mindstick.com/forum/33812/getting-number-of-items-selected-in-uicollectionview-in-ios) and displays them dynamically.

## Replies

### Reply by Aryan Kumar

In [Razor](https://www.mindstick.com/articles/12002/asp-dot-net-mvc5-razor-with-jquery), you can create a [view](https://yourviews.mindstick.com/view/84701/layoffs-in-google-india-2023-view) that iterates over a collection of items and displays them dynamically using a **foreach** loop or the **@foreach** directive. Here's a step-by-step guide on how to create such a view:

**Create a Razor View**: Start by creating a Razor view file in your ASP.NET Core or ASP.NET MVC application. Typically, Razor views have the ".cshtml" extension.

**Model Preparation**: Make sure you have a collection of items in your model that you want to iterate over. Ensure your view is strongly typed to that model.

Example model (C# class):

```plaintext
public class MyViewModel
{
    public List<string> Items { get; set; }
}
```

**Define the Model for Your View**: At the top of your Razor view, specify the model type using the **@model** directive. This allows you to access the properties of the model in the view.

```plaintext
@model MyNamespace.MyViewModel
```

**Iterate Over the Collection**: Use a **foreach** loop or the **@foreach** directive to iterate over the collection in your view.

Example using **foreach** loop:

```plaintext
<ul>
    @foreach (var item in Model.Items)
    {
        <li>@item</li>
    }
</ul>
```

Example using **@foreach** directive:

```plaintext
<ul>
    @foreach (var item in Model.Items)
    {
        <li>@item</li>
    }
</ul>
```

**Display the Items Dynamically**: Within the loop, you can dynamically display the items or apply any HTML or CSS as needed.

In the examples above, we iterate through a list of items and display them as list items within an unordered list (**<ul>**).

**Run the Application**: Once you have created your view, run your ASP.NET application, and navigate to the view to see the dynamically generated content based on your collection of items.

By following these steps, you can create a Razor view that iterates over a collection of items and displays them dynamically. This is a common approach for rendering dynamic content, such as lists or tables, in ASP.NET applications.


---

Original Source: https://www.mindstick.com/forum/160239/how-to-create-a-razor-view-that-iterates-over-a-collection-of-items-and-displays-them-dynamically

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
