Implementing a switch statement in a CSHTML page

asp.net Mvcasp.net Mvc-3Razor

asp.net Mvc Problem Overview


I'm trying to do something different. I have a view that contains an Id. Based on the value of the Id I want to change my heading that appears. Something like:

@{ switch id
   case "test": @;<h1>Test Site</h1>
   case "prod": @:<h1>Prod Site</h1>
   break;
}

I have quite a lot of case conditions so I though use of case would be best. Can anyone suggest how I can do this and get it to work? I am getting a lot of syntax errors so I think maybe it's not coded well.

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

Your switch needs to be completely enclosed in a block and it needs to be "broken" properly:

// Use the @{ } block and put all of your code in it
@{
    switch(id)
    {
        case "test":
            // Use the text block below to separate html elements from code
            <text>
                <h1>Test Site</h1>
            </text>
            break;  // Always break each case
        case "prod":
            <text>
                <h1>Prod Site</h1>
            </text>
            break;
        default:
            <text>
                <h1>WTF Site</h1>
            </text>
            break;                   
    }
}

Because the <h1> tags are enclosed html blocks by themselves, you may not need the <text> blocks for separation. It's just my habit to include them.

Solution 2 - asp.net Mvc

@switch (id)
{
    case "test": <h1>Test Site</h1>
    break;
    case "prod": <h1>Prod Site</h1>
    break;
}

There is no need to enclose the entire switch statement in a @{} block, (unlike Joel Etherton's post)

Your errors are basically regular syntax errors and have nothing to do with razor;

  1. the variable wasn't in parenthesis

  2. the body of switch wasn't in brackets

  3. no "break" after the first statement.

Solution 3 - asp.net Mvc

This doesn't answer your question, as indicated by the question's title, but it does solve the problem you described in the body of the question.

Use a view model class as the view's model and add a method that includes the switch statement. Then just call the method from the view via @Model.MethodWithSwitchStatement(). [The id can be saved in the view model object.]

Solution 4 - asp.net Mvc

@{
    String txt;
    switch (id) {
        case "test":
            txt = "Test";
            break;
        case "prod":
            txt = "Prod";
            break;
        default:
            txt = "WTF";
    }
}

<h1>@txt Site</h1>

The Most Concise: Less redundant or repetitive code and markup.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarianneView Question on Stackoverflow
Solution 1 - asp.net MvcJoel EthertonView Answer on Stackoverflow
Solution 2 - asp.net Mvcyoel halbView Answer on Stackoverflow
Solution 3 - asp.net MvcKenny EvittView Answer on Stackoverflow
Solution 4 - asp.net MvcMcClintView Answer on Stackoverflow