โœจ ASP.NET Razor Syntax
Estimated reading: 3 minutes 42 views

๐Ÿ” 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, and while loops 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 foreach for clean looping over lists or models
  • Format HTML elements inside loops (e.g., <li>, <tr>, <div>)
  • Use @index wisely 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 for when you need index control
  • Use foreach for 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ” Razor C# โ€“ Loops

Or Copy Link

CONTENTS
Scroll to Top