C# – Use switch case in MVC view

asp.net-mvcasp.net-mvc-4crazor

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.

Best Answer

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