如何显式停用Razor代码

时间:2013-08-12 18:52:16

标签: asp.net-mvc-4 razor

我使用Razor非常遵循这种模式:

@if (Model.IsAlreadyConfirmed)
{
     This account has already been confirmed.
}

但这不起作用,因为它期待代码。我被迫做这样的事情:

@if (Model.IsAlreadyConfirmed)
{
     @Html.Raw("This account has already been confirmed.")
}

或者这个:

@if (Model.IsAlreadyConfirmed)
{
    <span>This account has already been confirmed.</span>
}

另外,请随意纠正我不当使用行话。

2 个答案:

答案 0 :(得分:3)

<text>标记外,您还可以使用@:

为您的行添加前缀
@if (Model.IsAlreadyConfirmed)
{
     @:This account has already been confirmed.
}

答案 1 :(得分:1)

@if (Model.IsAlreadyConfirmed)
{
     <text>This account has already been confirmed.</text>
}

有一个名为text的特殊标签,只打印出你放入的内容。渲染的html中省略了实际标记。