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

Razor C# Logic


ASP.NET Razor - C# Logic Conditions


Programming Logic: Execute code based on conditions.


If conditional

C# allows the execution of code 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 in brackets
  • If the condition is true, the curly brackets The code inside is executed

Instance

@{var price=50;}
<html>
<body>
@if (price>30)
  {
  <p>The price is too high.</p>
  }
</body>
</html>

Run Instance»

Click the "Run Instance" button to view Online Example


Else condition

if statement can contain else condition.

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

Instance

@{var price=20;}
<html>
<body>
@if (price>30)
    {
    <p>The price is too high.</p>
    }
    else
    {    
    <p>The price is OK.</p>
    }
</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.


Else If condition

Multiple condition judgments can be usedelse if condition:

Example

@{var price=25;}
<html>
<body>
if (price>=30)
    {
    <p>The price is high.</p>
    }
    else if (price>20 && price<30)  
    {
    <p>The price is OK.</p>
    }
    else
    {
    <p>The price is low.</p>
    }
</body>
</html>

Run Instance»

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

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

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

else if The number of conditions is not limited.

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


Switch condition

The switch block can be used to test some individual conditions:

Example

@{
var message="";
var weekday=DateTime.Now.DayOfWeek;
var day=weekday.ToString()
}
<html>
<body>
@switch(day)
{
case "Monday":
    message="This is the first weekday.";
    break;
case "Thursday":
    message="Only one day before weekend.";
    break;
case "Friday":
    message="Tomorrow is weekend!";
    break;
default:
    message="Today is " + day;
    break;
}
<p>@message</p>
</body>
</html>

Run instance»

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

The test value (day) is written in brackets. Each individual test condition has a case value terminated by a semicolon and any number of lines of code terminated by a break statement. If the test value matches the case value, the corresponding line of code is executed.

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

php.cn