Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use switch case in MVC view

In MVC view I have a 'for' command that in each value I want to write specified tag.

I show you a simple case here:

@for (var i = 0; i < 4; i++)
{
    <div>
        @(switch (i)
        {
            case 0: ??? //write "<div>Custom Value 1</div>"
                     break;
            case 1: ??? //write "<span>Custom Value 2</span>"
                     break;
        })
    </div>
}

I use MVC4 Razor view.

Thanks for your time in advance.

like image 258
Hadi Sharifi Avatar asked Jan 17 '14 14:01

Hadi Sharifi


People also ask

Can we use Switch Case C#?

In C#, Switch statement is a multiway branch statement. It provides an efficient way to transfer the execution to different parts of a code based on the value of the expression. The switch expression is of integer type such as int, char, byte, or short, or of an enumeration type, or of string type.

Can we use and in switch case?

No, you cannot do something like case val_1 && val_2 . case statements only accept a single value.

Can a switch case have multiple values?

A switch statement using multiple value cases correspond to using more than one value in a single case. This is achieved by separating the multiple values in the case with a comma. Example 1: Go.


1 Answers

It's simple, you use your code same as this, It's works fine.

@for (var i = 0; i < 4; i++)
{
    <div>
        @switch (i)
        {
            case 0: 
                     <div>Custom Value 1</div>
                     break;
            case 1: 
                     <span>Custom Value 2</span>
                     break;
        }
    </div>
}
like image 106
user3206982 Avatar answered Sep 19 '22 18:09

user3206982