1. 条件语句:
使用 If、ElseIf 和 Else 语句执行条件判断:
@Code
Dim isAdmin As Boolean = True
Dim isLoggedIn As Boolean = False
End Code
@if (isAdmin) Then
<p>Welcome, Admin!</p>
ElseIf (isLoggedIn) Then
<p>Welcome, Logged-in User!</p>
Else
<p>Welcome, Guest!</p>
End If
2. 比较操作符:
使用比较操作符进行值的比较:
@Code
Dim age As Integer = 25
End Code
@if (age > 21) Then
<p>You are eligible for access!</p>
End If
3. 逻辑运算符:
使用逻辑运算符进行逻辑组合:
@Code
Dim isMember As Boolean = True
Dim hasSubscription As Boolean = False
End Code
@if (isMember AndAlso hasSubscription) Then
<p>You have access to premium content!</p>
ElseIf (isMember OrElse hasSubscription) Then
<p>You have limited access.</p>
Else
<p>You need to sign up for membership.</p>
End If
4. Select Case 语句:
使用 Select Case 语句进行多条件分支:
@Code
Dim userType As String = "Admin"
End Code
@Select Case userType
Case "Admin"
<p>Welcome, Admin!</p>
Case "User"
<p>Welcome, User!</p>
Case Else
<p>Unknown User Type</p>
End Select
5. 三元运算符:
使用三元运算符进行简单的条件赋值:
@Code
Dim isRegistered As Boolean = True
End Code
<p>User Status: @(If(isRegistered, "Registered", "Not Registered"))</p>
这些示例演示了在 Razor 页面中使用 VB 进行逻辑操作的不同方法。根据页面的需求,你可以选择使用适当的逻辑结构,以实现更复杂的页面行为和交互性。
转载请注明出处:http://www.zyzy.cn/article/detail/14860/ASP.NET Razor 标记