โœจ ASP.NET Razor Syntax
Estimated reading: 3 minutes 33 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 :

Leave a Reply

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

Share

๐Ÿ”ค Razor VB โ€“ Variables

Or Copy Link

CONTENTS
Scroll to Top