简体   繁体   English

剃刀HTML输出的三元

[英]Ternary for razor html output

This works but it's a lot of lines: 这行得通,但是有很多行:

if (foo == true)
{
    <td>Yes</td>
}
else
{
    <td>No</td>
}

This doesnt work because Only assignment, call, increment, decrement, and new object expressions can be used as a statement ... 这是行不通的,因为只有赋值,调用,递增,递减和新对象表达式可以用作语句 ...

<td>
    @{foo == true ? "Yes" : "No";}
</td>

Is there a simple solution for this? 有一个简单的解决方案吗?

Well, the problem is that you picked up the wrong parenthesis, this works: 好吧,问题在于您选择了错误的括号,这可以正常工作:

<td>
    @(foo == true ? "Yes" : "No")
</td>

Per the documentation : 根据文档

Explicit Razor expressions consist of an @ symbol with balanced parenthesis. 显式Razor表达式由带圆括号的@符号组成。 To render last week's time, the following Razor markup is used: 要渲染上周的时间,请使用以下Razor标记:

<p>Last week this time: @(DateTime.Now - TimeSpan.FromDays(7))</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM