๐ฃ 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
@Codeblocks for variable assignment - Use clear, readable
Ifblocks - Combine logic with loops for conditional formatting
โ Avoid:
- Writing deeply nested logic in
.vbhtml - Placing business rules inside view logic
- Omitting
End IforEnd 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,Elsefor general conditions Select Caseis great for multiple value branches- Inline
Ifexpressions 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 :
