Chapter 26: Razor VB Logic
Razor – VB Logic Conditions — that is, how you write if/else decisions, switch statements, inline conditions, and null checks when you are using VB.NET inside .vbhtml Razor files.
This is the VB counterpart to the C# logic conditions lesson we did earlier. Conceptually 100% the same, but syntax is noticeably different — and this is exactly where most people get confused when they look at VB Razor examples on W3Schools or legacy code.
I will explain it slowly, like we are sitting together comparing two open files side-by-side: one C# .cshtml and one VB .vbhtml — same logic, same HTML output, different language.
1. The Four Main Ways to Write Conditions in VB Razor
A. Multi-line @If … Then … Else … End If (Most common for blocks of HTML)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
@Code Dim hour As Integer = Now.Hour Dim isAdmin As Boolean = True ' imagine from User.IsInRole Dim cartCount As Integer = 0 ' imagine from Session or DB End Code <div class="greeting"> @If hour < 12 Then @<span>Good Morning! ☀️</span> ElseIf hour < 17 Then @<span>Good Afternoon! 🌤️</span> Else @<span>Good Evening! 🌙</span> End If @If isAdmin Then @<a href="/Admin/Dashboard" class="admin-link">Admin Panel</a> End If </div> @If cartCount > 0 Then @<span class="cart-badge">@cartCount</span> Else @<span class="cart-empty">Cart is empty</span> End If |
Key VB rules here:
- @If condition Then
- HTML starts right after Then (often with @<tag>)
- ElseIf (one word)
- Else (optional)
- Always end with End If
- No curly braces — VB uses End If to close blocks
B. Inline/single-line If(condition, trueValue, falseValue) — VB’s ternary
Very common for class names, text, attributes
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
@Code Dim inStock As Boolean = True Dim price As Decimal = 399 Dim discount As Integer = 20 Dim finalPrice As Decimal = price * (1 - discount / 100) End Code <button class="@(If(inStock, "btn-success", "btn-secondary disabled"))"> @(If(inStock, "Add to Cart – ₹" & finalPrice.ToString("N0"), "Out of Stock")) </button> <div class="stock @(If(inStock, "text-success", "text-danger"))"> @(If(inStock, "In Stock", "Currently Unavailable")) </div> |
→ If(… , … , … ) is VB’s version of C#’s condition ? … : …
C. Null handling – If(… Is Nothing, default, value) or ? operator (VB 14+)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
@Code Dim userName As String = Nothing ' imagine from session/DB Dim cartItems As List(Of String) = Nothing End Code <p>Hello @(If(userName Is Nothing, "Guest", userName))!</p> <!-- Null-conditional (VB 14+) --> <p>Cart has @(If(cartItems?.Count, 0)) items</p> <!-- Null-coalescing --> @Code userName = If(userName, "Anonymous") End Code <p>Welcome back, @userName!</p> |
→ Is Nothing = C#’s is null → ?. null-conditional operator works in VB 14+ (same as C#)
D. @Select Case … Case … End Select (VB’s switch)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
@Code Dim day As DayOfWeek = Now.DayOfWeek End Code <div class="day-message"> @Select Case day Case DayOfWeek.Monday @<p>Motivation Monday! 💪</p> Case DayOfWeek.Friday @<p>Friday vibes! 🎉</p> Case DayOfWeek.Saturday, DayOfWeek.Sunday @<p>Weekend mode ON! 🏖️</p> Case Else @<p>Keep going — it's @day!</p> End Select </div> |
→ Select Case is VB’s cleaner version of switch → Case value1, value2 allows multiple matches → Case Else = default
3. Full Realistic Example – VB Product Card with Many Conditions
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
@Code Dim product = New With { .Name = "Hyderabadi Dum Biryani (Family Pack)", .Price = 699D, .DiscountPercent = 15, .Rating = 4.7, .ReviewCount = 842, .IsInStock = True, .IsNew = True, .Tags = New String() {"Non-Veg", "Spicy", "Bestseller"} } Dim finalPrice = product.Price * (1 - product.DiscountPercent / 100) End Code <div class="product-card"> @If product.IsNew Then @<span class="badge-new">NEW</span> End If <h3>@product.Name</h3> <div class="price-block"> <span class="current-price">₹@finalPrice.ToString("N0")</span> @If product.DiscountPercent > 0 Then @<span class="original-price">₹@product.Price.ToString("N0")</span> @<span class="discount">(@product.DiscountPercent% OFF)</span> End If </div> <div class="rating"> @For i As Integer = 1 To 5 @<span class="@(If(i <= Math.Round(product.Rating), "star filled", "star empty"))">★</span> Next <span>(@product.ReviewCount)</span> </div> <p class="stock @(If(product.IsInStock, "in-stock", "out-of-stock"))"> @(If(product.IsInStock, "In Stock – Ready to Ship", "Out of Stock")) </p> <div class="tags"> @For Each tag In product.Tags @<span class="tag">@tag</span> Next </div> @If product.IsInStock Then @<button class="btn-add">Add to Cart</button> Else @<button class="btn-notify" disabled>Notify Me When Available</button> End If </div> |
4. VB Razor Conditions – Quick Reference Table
| Task | VB Razor Syntax | C# Razor equivalent (comparison) |
|---|---|---|
| Multi-line if | @If … Then … ElseIf … Else … End If | @if (…) { … } else if (…) { … } |
| Inline/ternary | If(condition, true, false) | condition ? true : false |
| Null check | If(x Is Nothing, default, x) | x ?? default |
| Null-conditional (VB 14+) | x?.Property | x?.Property |
| Switch | @Select Case … Case … Case Else … End Select | @switch (…) { case … : … break; } |
| Boolean literals | True / False | true / false |
5. Teacher Summary – VB Logic Conditions in Razor
ASP.NET Razor – VB Logic Conditions means:
- Multi-line decisions → @If … Then … Else … End If
- Inline decisions → If(…, …, …)
- Null safety → Is Nothing, If(…, default, …), ?.
- Multiple choices → @Select Case … End Select
- All conditions can control HTML output, class names, button visibility, text, etc.
- VB style is more verbose (End If, Then, capital True/False) but very readable if you come from Classic ASP/VBA
You now understand both C# and VB Razor condition styles — very useful for reading legacy code or comparing W3Schools examples.
Next step?
- Want to see conditions + loops together in VB (e.g., conditional list rows)?
- Or VB Razor forms (validation, pre-filled values)?
- Or convert a full product page (loops + conditions) from C# to VB?
- Or finally move to modern ASP.NET Core Razor Pages (almost always C#)?
Just tell me — you’re doing fantastic work! Keep shining from Hyderabad! 🚀🇮🇳 😊
