Razor If/Else 条件运算符语法

运气不太好,我在 Razor 中有以下 if/else 语句,它可以很好地工作

<small>
@if(deletedView){
@:Deleted
}
else {
@:Created
} by
</small>

我正在尝试做这样的事情:

<small>
@(deletedView) ? @:Deleted : @:Created by
</small>

但是这种方法失败了,正确的语法是什么?

133101 次浏览

You need to put the entire ternary expression in parenthesis. Unfortunately that means you can't use "@:", but you could do something like this:

@(deletedView ? "Deleted" : "Created by")

Razor currently supports a subset of C# expressions without using @() and unfortunately, ternary operators are not part of that set.