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

Razor VB โ€“ Logic โ€“ Control Flow with If, Else, and Select Case in Razor Views

Introduction โ€“ What Is Razor VB Logic?

In Razor using VB.NET, logic statements such as If, Else, ElseIf, and Select Case let you control which parts of the page are rendered based on dynamic conditions. This enables conditional rendering, role-based display, and dynamic styling in .vbhtml pages.

In this guide, youโ€™ll learn:

  • How to write conditional logic in Razor VB views
  • Use of If...ElseIf, Select Case, and inline conditionals
  • Combine logic with HTML for smart UI rendering
  • Best practices for clean, readable control flow

Basic If Statement

@Code
    Dim isMember As Boolean = True
End Code

@if isMember Then
    <p>Welcome, valued member!</p>
End If

Output:
Welcome, valued member!


If...ElseIf...Else Statement

@Code
    Dim score As Integer = 82
End Code

@if score >= 90 Then
    <p>Grade: A</p>
ElseIf score >= 80 Then
    <p>Grade: B</p>
Else
    <p>Grade: C</p>
End If

Output:
Grade: B


Select Case Statement

@Code
    Dim role As String = "Editor"
End Code

@Select Case role
    Case "Admin"
        <p>Admin access granted.</p>
    Case "Editor"
        <p>You can edit content.</p>
    Case Else
        <p>View-only mode.</p>
End Select

Output:
You can edit content.


โ” Inline Conditional (Ternary-Like) with If

@Code
    Dim isPremium As Boolean = False
    Dim label As String = If(isPremium, "Premium User", "Free User")
End Code

<p>@label</p>

Output:
Free User


Logic Inside Loops

@Code
    Dim prices As Integer() = {100, 200, 300}
End Code

@For Each price In prices
    If price > 150 Then
        <p>High Price: @price</p>
    Else
        <p>Budget Price: @price</p>
    End If
Next

HTML Attribute with VB Logic

@Code
    Dim isActive As Boolean = True
End Code

<button class="btn @(If(isActive, "btn-primary", "btn-secondary"))">Click Me</button>

Output:
A button with class btn btn-primary if isActive is true.


Best Practices for Razor VB Logic

Do:

  • Use @Code blocks for variable assignment
  • Use clear, readable If blocks
  • Combine logic with loops for conditional formatting

Avoid:

  • Writing deeply nested logic in .vbhtml
  • Placing business rules inside view logic
  • Omitting End If or End Select

Summary โ€“ Recap & Next Steps

Razor VB logic allows your pages to respond to runtime conditions and render content selectively using VB.NET syntax. Whether using If, Else, or Select Case, Razor VB makes condition-based markup both expressive and powerful.

Key Takeaways:

  • Use If, ElseIf, Else for general conditions
  • Select Case is great for multiple value branches
  • Inline If expressions simplify HTML attribute logic

Real-world Use Cases:

  • Show UI elements based on login status
  • Render messages or pricing tiers dynamically
  • Add conditional CSS classes

FAQs โ€“ Razor VB Logic


Can I use ElseIf in Razor VB?
Yes. Razor VB fully supports ElseIf, following VB.NET syntax.


How do I conditionally render classes or HTML in Razor VB?
Use inline If expressions within attributes:

class="@(If(condition, "enabled", "disabled"))"

Is Razor VB case-sensitive?
No. Like standard VB.NET, Razor VB is case-insensitive.


Share Now :
Share

๐Ÿ”ฃ Razor VB โ€“ Logic

Or Copy Link

CONTENTS
Scroll to Top