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
    case value2:
        // Code to execute when the expression matches value2
    // Additional case statements...
        // Code to execute when no case matches the expression

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";

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

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
    case "user":
        <p>Welcome, User!</p>
        // Additional user-specific logic
        <p>Welcome, Guest!</p>
        // Additional guest-specific logic

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.

Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

2 + 4 =

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