剃须刀的if / else条件运算符语法 [英] Razor If/Else conditional operator syntax

查看:148
本文介绍了剃须刀的if / else条件运算符语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运气不好,我有以下的,如果在剃刀/ else语句这完美的作品

Not having much luck, I have the following if/else statement in Razor which works perfectly

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

我试图做这样的事情:

I am trying to do something like this:

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

但失败草草收场。什么是正确的语法?

But that fails miserably. What is the proper syntax?

推荐答案

您需要把整个三元前pression括号。不幸的是,这意味着你不能使用@,但你可以做这样的事情:

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")

目前剃刀支持C#前pressions一个子集,而无需使用@(),不幸的是,三元运营商都不是集的一部分。

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

这篇关于剃须刀的if / else条件运算符语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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