Chapter 22: Razor – C# Loops and Arrays

Razor: C# Loops and Arrays inside .cshtml files.

This is where your pages stop being static and start becoming truly dynamic: showing lists of products, menu items, search results, user comments, table rows, cards in a gallery, tags, pagination numbers — anything that repeats.

I’m going to explain it like we’re building a real Hyderabad food delivery website together — slowly, step by step, with lots of examples, common patterns, and the little tricks that make loops in Razor feel clean and readable.

1. Why Loops & Arrays Are So Important in Razor

In almost every real website you build:

  • You have collections (arrays, lists, query results from database)
  • You need to repeat HTML for each item (product card, table row, <li>, <option>, etc.)
  • You often need index, conditional styling (even/odd rows, first/last item), or summaries (total price, count)

Razor + C# makes this beautifully simple — no ugly string concatenation or manual HTML building.

2. The Two Most Common Loop Types in Razor

A. @foreach — Loop over a collection (90% of cases)

HTML

Output HTML (what browser sees):

HTML

Key points:

  • var food — each item in turn
  • Curly braces { … } contain the repeated HTML
  • Razor automatically switches back to HTML mode after the block

B. @for — When you need index / position / step

HTML

→ Use @for when you need:

  • Index (i)
  • Step/count control
  • Access by index (foods[i])
  • First/last styling

3. Real-World Example – Product Cards from Database/Array

Let’s make it realistic — imagine data from a database query.

HTML

→ This is typical Razor markup with loops:

  • @foreach repeats the card HTML
  • Variables inside (p.Name, p.Price)
  • Conditionals (@if (p.InStock)) change button/content
  • Dynamic classes (class=”@stockClass”)
  • Formatting (ToString(“N2”) for money)

4. Bonus Patterns You’ll Use Every Day

Pattern 1: Alternating row styles (even/odd)

HTML

Pattern 2: Show “No items” message

HTML

Pattern 3: First / Last item special treatment

HTML

5. Teacher Summary – What to Remember

ASP.NET Razor – C# Loops and Arrays means:

  • Use arrays / List<T> / IEnumerable<T> from DB/query/form
  • @foreach — simplest and most common (no index needed)
  • @for — when you need position/index/count/step
  • Inside loop → normal HTML + @item.Property + @if / @(expression)
  • Use LINQ helpers like .Any(), .Count(), .Select() inside @{ } blocks
  • Always check for empty collections to avoid blank pages

Master these 2 loops + List<T> / var[] and you can display any repeating content beautifully.

Next class?

  • Want to see pagination with loops (show 10 items, page links)?
  • Or nested loops (categories → products)?
  • Or how loops work with WebGrid (from earlier lessons)?
  • Or jump to @model and strongly-typed collections?

Tell me — you’re building real skills here in Hyderabad! Keep going strong! 🚀🇮🇳

You may also like...

Leave a Reply

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