条件语句:
1. if-else 语句:
@{
Dim condition As Boolean = True
}
@if condition Then
<p>This is true.</p>
Else
<p>This is false.</p>
End If
2. Select Case 语句:
@{
Dim option As String = "B"
}
Select Case option
Case "A"
<p>Option A selected.</p>
Case "B"
<p>Option B selected.</p>
Case Else
<p>No option selected.</p>
End Select
循环:
1. For Each 循环:
@{
Dim names As New List(Of String) From {"Alice", "Bob", "Charlie"}
}
<ul>
@For Each name As String In names
<li>@name</li>
Next
</ul>
2. For 循环:
<ul>
@For i As Integer = 1 To 5
<li>Item @i</li>
Next
</ul>
3. While 循环:
@{
Dim count As Integer = 0
}
<ul>
@While count < 3
<li>Item @count</li>
count += 1
End While
</ul>
逻辑运算符:
@{
Dim isLoggedIn As Boolean = True
Dim isAdmin As Boolean = False
}
@if isLoggedIn AndAlso isAdmin Then
<p>Welcome, Administrator!</p>
ElseIf isLoggedIn Then
<p>Welcome, User!</p>
Else
<p>Please log in.</p>
End If
函数调用:
@{
Function FormatDate(ByVal dateValue As DateTime) As String
Return dateValue.ToString("yyyy-MM-dd")
End Function
}
<p>Formatted Date: @FormatDate(DateTime.Now)</p>
这些例子展示了如何在Razor中使用VB代码进行逻辑控制。你可以根据需要灵活地使用VB语法,根据条件生成不同的HTML内容。在Razor中,C#和VB的语法在使用上是相似的,你可以根据自己的熟悉程度或团队的技术栈来选择其中之一。
转载请注明出处:http://www.zyzy.cn/article/detail/14707/ASP.NET