๐ Razor C# โ Loops โ Iterate Through Data in Razor Views
๐งฒ Introduction โ Why Use Loops in Razor?
Loops in Razor syntax allow you to repeat HTML output based on C# data structures like arrays, lists, or object collections. Whether you’re displaying a list of users, products, or table rows, Razor loops help you generate dynamic, structured content easily inside .cshtml files.
๐ฏ In this guide, youโll learn:
- How to use for,foreach, andwhileloops in Razor
- Real-world examples of list rendering
- Looping with conditions and counters
- Best practices for loop-based UI rendering in ASP.NET
๐ Razor for Loop Example
@{
    for (int i = 1; i <= 5; i++)
    {
        <p>Item number @i</p>
    }
}
๐งช Output:
Item number 1  
Item number 2  
Item number 3  
Item number 4  
Item number 5
๐ Razor foreach Loop โ Ideal for Collections
@{
    var fruits = new[] { "Apple", "Banana", "Cherry" };
}
<ul>
@foreach (var fruit in fruits)
{
    <li>@fruit</li>
}
</ul>
๐งช Output:
- Apple
- Banana
- Cherry
๐ Razor while Loop
@{
    int count = 1;
    while (count <= 3)
    {
        <p>Loop count: @count</p>
        count++;
    }
}
๐ Loop with Index & Conditional Logic
@{
    var colors = new[] { "Red", "Green", "Blue" };
}
<ol>
@for (int i = 0; i < colors.Length; i++)
{
    if (i == 0)
    {
        <li><strong>@colors[i] (First)</strong></li>
    }
    else
    {
        <li>@colors[i]</li>
    }
}
</ol>
๐งช Output:
- Red (First)
- Green
- Blue
๐งฉ Loop Over Model Data
Assume your view is strongly typed:
@model List<string>
โ Razor Code
<ul>
@foreach (var name in Model)
{
    <li>@name</li>
}
</ul>
โ Controller Code (ASP.NET MVC)
public IActionResult Index()
{
    var users = new List<string> { "Alice", "Bob", "Charlie" };
    return View(users);
}
๐งฌ Nested Loops Example
@{
    var matrix = new int[,] { {1,2}, {3,4}, {5,6} };
}
@for (int i = 0; i < 3; i++)
{
    <p>
    @for (int j = 0; j < 2; j++)
    {
        @matrix[i, j] 
    }
    </p>
}
๐งช Output:
1 2  
3 4  
5 6
๐ Best Practices for Razor Loops
โ Do:
- Use foreachfor clean looping over lists or models
- Format HTML elements inside loops (e.g., <li>,<tr>,<div>)
- Use @indexwisely to control output (e.g., alternating row styles)
โ Avoid:
- Writing heavy business logic inside Razor loops
- Modifying data structures during loop execution
- Using loops without checking for null or empty collections
๐ Summary โ Recap & Next Steps
Razor loops help render dynamic, repeated content using familiar C# constructs like for, foreach, and while. Whether iterating over arrays or models, Razor ensures clean and readable output.
๐ Key Takeaways:
- Use forwhen you need index control
- Use foreachfor simple data iteration
- Use loops to generate clean HTML for lists, tables, cards, etc.
โ๏ธ Real-world Use Cases:
- Display product grids or user lists
- Render table rows dynamically
- Apply CSS classes based on index
โ FAQs โ Razor Loops in C#
โ Can I use nested loops in Razor views?
โ
 Yes. Razor supports full C# syntax including nested for or foreach loops.
โ Should I use loops in controller or Razor view?
โ
 Use Razor for UI rendering loops. Perform business logic and filtering in the controller.
โ How do I alternate row colors in a loop?
โ
 Use the loop index:
<tr class="@(i % 2 == 0 ? "even" : "odd")">
Share Now :
