Razor If/Else 条件运算符语法 [英] Razor If/Else conditional operator syntax

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

问题描述

运气不佳,我在 Razor 中有以下 if/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?

推荐答案

您需要将整个三元表达式放在括号中.不幸的是,这意味着您不能使用@:",但您可以这样做:

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 目前支持 C# 表达式的一个子集,而不使用 @(),不幸的是,三元运算符不属于该集合.

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

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

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