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

Razor VB โ€“ Variables โ€“ Declare and Use VB.NET Variables in Razor Views

Introduction โ€“ What Are Razor VB Variables?

In Razor using VB.NET, variables allow you to store and work with dynamic data in .vbhtml pages. You can declare variables inside code blocks, assign values, and use them within your HTML output just like in traditional VB.NET applications.

In this guide, youโ€™ll learn:

  • How to declare and use variables in Razor VB
  • Inline vs block usage
  • Examples for strings, numbers, arrays, and conditions
  • Best practices and output formatting

Declaring Variables in Razor VB

Use @Code ... End Code blocks to declare and assign variables.

@Code
    Dim name As String = "Vaibhav"
    Dim age As Integer = 28
End Code

<p>Name: @name</p>
<p>Age: @age</p>

Output:

Name: Vaibhav  
Age: 28

Inline Expressions

You can print values using @variableName:

<p>Current Year: @DateTime.Now.Year</p>

Example โ€“ Sum of Two Variables

@Code
    Dim a As Integer = 10
    Dim b As Integer = 15
    Dim sum As Integer = a + b
End Code

<p>Total: @sum</p>

Output: Total: 25


Arrays and Collections

@Code
    Dim colors As String() = {"Red", "Green", "Blue"}
End Code

<ul>
@For Each color In colors
    @<li>@color</li>
Next
</ul>

Output:

  • Red
  • Green
  • Blue

Conditional Variable Assignment

@Code
    Dim userType As String = "admin"
    Dim message As String = If(userType = "admin", "Welcome Admin", "Welcome Guest")
End Code

<p>@message</p>

Output: Welcome Admin


Using Built-in VB Functions

@Code
    Dim input As String = "hello world"
    Dim upper As String = input.ToUpper()
End Code

<p>@upper</p>

Output: HELLO WORLD


Best Practices for Razor VB Variables

Do:

  • Keep variable logic simple and readable
  • Use @Code blocks for declaration, @var for rendering
  • Prefer controller/viewmodel logic for business operations

Avoid:

  • Overusing logic inside views
  • Redeclaring variables that already exist in model or ViewBag
  • Skipping type declarations if clarity is needed

Summary โ€“ Recap & Next Steps

Razor VB makes it easy to declare and use VB.NET variables within views using a syntax familiar to VB developers. Whether rendering text, numbers, or arrays, variables make views dynamic and responsive.

Key Takeaways:

  • Use @Code blocks to declare variables
  • Output values with @variable
  • Combine with loops and conditions for interactive UI

Real-world Use Cases:

  • Displaying user details or dashboard values
  • Creating reusable formatting or conditional output
  • Looping through database records passed from controller

FAQs โ€“ Razor VB Variables


Can I use Dim in Razor VB?
Yes. Razor VB follows standard VB.NET syntax including Dim, If, For, etc.


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


Can I use @Code inside HTML tags?
No. Use @variable for inline output and @Code for logic blocks.


Share Now :
Share

๐Ÿ”ค Razor VB โ€“ Variables

Or Copy Link

CONTENTS
Scroll to Top