我正在尝试检查一个 bool 值,然后显示一个整数:
@( ViewBag.HaveBeenHere ? submission.DurationInMonths )
我收到一个错误:
CS1003: Syntax error, ':' expected
我知道 : 用于 else,但在这种情况下我没有 else。
当我这样添加时:
@( ViewBag.HaveBeenHere ? submission.DurationInMonths : "" )
我收到这个错误:
CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and 'string'
如何使用简写 if 语句来检查 bool 值并在 View 中显示整数?
最佳答案
如果你想保留语法,你可以这样做:
@( ViewBag.HaveBeenHere ? submission.DurationInMonths.ToString() : "" )
当然,添加 ToString()
将使返回类型相同。
关于c# - Razor View 中的简写 If without else (ASP.NET MVC4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23275949/