Razor Tutoriallogin
Razor Tutorial
author:php.cn  update time:2022-04-11 14:21:21

Razor VB Logic


ASP.NET Razor - VB Logic Conditions


Programming Logic: Execute code based on conditions.


If Condition

VB allows code execution based on conditions.

Use if statement to determine conditions. According to the judgment result, the if statement returns true or false:

  • The if statement starts a code block
  • The condition is written between if and then
  • If the condition is true , the code between if...then and end if is executed

Instance

@Code
Dim price=50
End Code
<html>
<body>
@If price>30 Then
@<p>The price is too high.</p>
End If
</body>
</html>

Running Example»

Click the "Run Instance" button to view the online instance


Else condition

if statement can contain else condition.

else condition defines the code to be executed when the condition is false.

Instance

@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>
</html>

Run Instance»

Click the "Run Instance" button to view the online instance

Note: In the above example, if the first condition is true, the code of the if block will be executed. The else condition covers "everything else" except the if condition.


ElseIf condition

Multiple condition judgments can be usedelseif condition:

Instance

@Code
Dim price=25
End Code
<html>
<body>
@if price>=30 Then
    @<p>The price is high.</p>
ElseIf price>20 And price<30 then  
    @<p>The price is OK.</p>
Else
    @<p>The price is low.</p>
End If
</body>
</html>

Run instance»

Click the "Run instance" button to view the online instance

In the above example, if the first condition is true, if The code of the block will be executed.

If the first condition is not true and the second condition is true, the code in the elseif block will be executed.

elseif There is no limit to the number of conditions.

If neither the if nor elseif conditions are true, the final else block (without the condition) covers "everything else".


Select condition

select block can be used to test some individual conditions:

Example

@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>

Run instance»

Click the "Run instance" button to view the online instance

"Select Case" followed by the test value (day) . Each individual test condition has a case value and any number of lines of code. If the test value matches the case value, the corresponding line of code is executed.

The select block has a default case (Case Else), which overrides "all other cases" when none of the specified cases match.


php.cn