Implement Switch Statement in .NET MVC Razor CSHTML Pages

In this blog post, we will learn Switch statement in .NET MVC Razor CSHTML pages, exploring its syntax, functionality, and providing code examples to illustrate its usage.

What is the Switch Statement?

The switch statement is a control flow statement that allows you to select and execute one of many code blocks based on the value of a given expression or variable. It provides a concise and readable way to handle multiple cases without the need for lengthy if-else if chains.

In .NET MVC Razor CSHTML pages, the switch statement can be particularly useful when you need to render different HTML or apply different logic based on specific conditions or variables.

Syntax and Usage

The syntax of a switch statement in C# is as follows:

switch (expression)
{
    case value1:
        // Code to execute when the expression matches value1
        break;
    case value2:
        // Code to execute when the expression matches value2
        break;
    // Additional case statements...
    default:
        // Code to execute when no case matches the expression
        break;
}

Here's a breakdown of the different components:

  • expression: The value or variable used to determine which code block to execute.
  • case: Each case statement represents a specific value or condition to check against the expression.
  • value1, value2: The values or conditions that are compared with the expression.
  • default: An optional case that is executed when none of the previous cases match the expression.

Code Examples

Let's explore a few examples to understand how the switch statement can be used in .NET MVC Razor CSHTML pages:

Example 1: Rendering HTML based on a variable

@{
    var status = "completed";
}

<p>
    @switch (status)
    {
        case "completed":
            <span style="color: green;">Completed</span>
            break;
        case "pending":
            <span style="color: orange;">Pending</span>
            break;
        default:
            <span style="color: red;">Unknown</span>
            break;
    }
</p>

In this example, the switch statement is used to determine the status of a task and render HTML accordingly. If the status variable is "completed," it will display "Completed" in green. If it is "pending," it will display "Pending" in orange. Otherwise, it will display "Unknown" in red.

Example 2: Executing different logic based on a condition

@{
    var role = "admin";
}

@switch (role)
{
    case "admin":
        <p>Welcome, Administrator!</p>
        // Additional admin-specific logic
        break;
    case "user":
        <p>Welcome, User!</p>
        // Additional user-specific logic
        break;
    default:
        <p>Welcome, Guest!</p>
        // Additional guest-specific logic
        break;
}

In this example, the switch statement is used to execute different logic based on the user's role. If the role variable is "admin," it will display a welcome message for administrators and execute additional admin-specific logic. Similarly, it will display different messages and execute specific logic for "user" and "guest" roles.

 
Asp.Net Ajax Control Toolkit tutorials.

Give your valuable comments.

Name
Email
Comment
7 + 7 =
 

About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice