ASP.NET Razor - VB 邏輯條件
編程邏輯:執(zhí)行基于條件的代碼。
If 條件
VB 允許您執(zhí)行基于條件的代碼。
如需測試某個條件,您可以使用 if 語句。if 語句會基于您的測試來返回 true 或 false:
- if 語句啟動代碼塊
- 條件位于 if 和 then 之間
- 如果條件為真,則執(zhí)行 if ... then 與 end if 之間的代碼
實例
@Code
Dim price=50
End Code
<html>
<body>
@If price>30 Then
@<p>The price is too high.</p>
End If
</body>
</html>
運行實例
Else 條件
if 語句能夠包含 else 條件。
else 條件定義條件為 false 時執(zhí)行的代碼。
實例
@Code
Dim price=20
End Code
<html>
<body>
@if price>30 then
@<p>The price is too high.</p>
Else
@<p>The price is OK.</p>
End If
</body>
</htmlV>
運行實例
注釋:在上面的例子中,如果價格不大于 30,則執(zhí)行其余的語句。
ElseIf 條件
可通過 else if 條件來測試多個條件:
實例
@Code
Dim price=25
End Code
<html>
<body>
@If price>=30 Then
@<p>The price is high.</p>
ElseIf price>20 And price<30
@<p>The price is OK.</p>
Else
@<p>The price is low.</p>
End If
</body>
</html>
運行實例
在上面的例子中,如果第一個條件為 true,則執(zhí)行第一個代碼塊。
否則,如果下一個條件為 true,則執(zhí)行第二個代碼塊。
您能夠設(shè)置任意數(shù)量的 else if 條件。
如果 if 和 else if 條件均不為 true,則執(zhí)行最后一個 else 代碼塊。
Select 條件
select 代碼塊可用于測試一系列具體的條件:
實例
@Code
Dim weekday=DateTime.Now.DayOfWeek
Dim day=weekday.ToString()
Dim message=""
End Code
<html>
<body>
@Select Case day
Case "Monday"
message="This is the first weekday."
Case "Thursday"
message="Only one day before weekend."
Case "Friday"
message="Tomorrow is weekend!"
Case Else
message="Today is " & day
End Select
<p>@message</p>
</body>
</html>
運行實例
"Select Case" 之后是測試值 (day)。每個具體的測試條件以 case 關(guān)鍵詞開頭,其后允許任意數(shù)量的代碼行。如果測試值匹配 case 值,則執(zhí)行代碼行。
select 代碼塊可為其余的情況設(shè)置默認的 case (default:),允許在所有 case 均不為 true 時執(zhí)行代碼。