实现在CSHTML页面switch语句 [英] Implementing a switch statement in a CSHTML page

查看:1122
本文介绍了实现在CSHTML页面switch语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试做不同的东西。我有一个包含ID的视图。基于ID的价值我想改变我的标题出现。是这样的:

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

我有相当多的情况下的条件,使我虽然用例将是最好的。任何人都可以建议我怎么能做到这一点,得到它的工作?我得到了很多语法错误,所以我想,也许这不是codeD好。

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.

推荐答案

您需要将交换机完全封闭在一个块中,它需要破正确的:

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

由于该&LT; H1&GT; 标记本身封闭的HTML代码,你可能不需要&LT;文本&GT; 分离块。这只是我的习惯,包括他们。

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.

这篇关于实现在CSHTML页面switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆