Chapter 25: Razor VB Loops

Razor – VB Loops and Arrays — that is, how you work with repeating content and collections (arrays, lists, etc.) when you are writing Razor pages in VB.NET (.vbhtml files).

This is the VB.NET version of the C# loops & arrays lesson we did earlier. The concepts are exactly the same:

  • you have a collection of items
  • you want to repeat some HTML for each item
  • sometimes you need position/index, alternating styles, empty checks, etc.

But the syntax is very different — and that’s where most people get stuck when they switch between C# and VB examples.

I will explain it slowly, step-by-step, like we are building a real page together — and I will show many complete examples so you can see exactly how it looks and feels in VB Razor.

1. The Two Main Loop Constructs in VB Razor

A. @For Each … Next

→ This is the most common loop — used whenever you want to go through every item in a collection.

B. @For … Next (with counter)

→ Used when you need the position / index or want to control the step/count.

Both are placed inside HTML and repeat the HTML block they contain.

2. Basic @For Each Example – Simple List

HTML

Rendered HTML:

HTML

Important VB Razor loop rules:

  • @For Each item In collection
  • The repeated HTML starts right after the @For Each line
  • Use @<tag>…</tag> or just plain HTML
  • End with Next (no curly braces!)

3. @For Each with Conditions and Formatting

HTML

Key VB points here:

  • If … Then … Else … End If (multi-line)
  • If(condition, trueValue, falseValue) (inline / ternary)
  • No parentheses needed around conditions in single-line If
  • @<tag> is often used when starting HTML right after @If or @For Each

4. @For … Next – When You Need Index / Position

HTML

Important notes:

  • For i As Integer = 0 To foods.Count – 1
  • Use foods(i) to access by index (parentheses in VB)
  • To is inclusive — so To 4 means 0,1,2,3,4

5. Checking for Empty Collections (Very Important Pattern)

HTML

6. Full Realistic Example – Menu with Active Item Highlight

HTML

7. VB Razor Loops & Arrays – Quick Reference Table

Task VB Razor Syntax C# Razor equivalent (for comparison)
Loop over collection @For Each x In list … Next @foreach (var x In list) { … }
Loop with index @For i = 0 To list.Count – 1 … Next @for (int i = 0; i < list.Count; i++) { … }
Access by index list(i) list[i]
Inline if If(condition, true, false) condition ? true : false
Multi-line if If … Then … Else … End If if (…) { … } else { … }
Empty check If items.Any() Then or If items.Count > 0 Then if (items.Any())
Create list New List(Of String) From { … } new List<string> { … }

8. Teacher Closing Thoughts

ASP.NET Razor – VB Loops and Arrays means:

  • Use @For Each item In collection … Next for simple iteration (most common)
  • Use @For i = start To end … Next when you need index or position
  • Repeat HTML inside the loop — same as C#
  • VB uses If … Then … End If, For Each … Next, list(i), & for strings
  • Always check for empty collections (items.Any() or items.Count > 0)
  • All the same patterns you learned in C# (alternating rows, first/last, no-items message) work exactly the same — only syntax changes

You now understand both C# and VB Razor looping styles — very useful for reading old code or W3Schools examples.

Next questions?

  • Want to see pagination with VB loops?
  • Or nested loops (categories → products)?
  • Or convert a full product grid from C# to VB?
  • Or move forward to modern Razor Pages (which is almost always C#)?

Just tell me — you’re doing excellent work! Keep going strong from Hyderabad! 🚀🇮🇳 😊

You may also like...

Leave a Reply

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