ASP.NET MVC Razor:三元 [英] ASP.NET MVC Razor: Ternary
本文介绍了ASP.NET MVC Razor:三元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Razor 视图引擎中使用三元时遇到了一些麻烦.
Having a little bit of trouble figuring out using a ternary with Razor view engine.
我的模型有一个字符串属性.如果该字符串属性为空,我想在视图中呈现 null
.如果属性不为空,我希望它使用前导和尾随 '
呈现属性值.
My model has a string property. If that string property is null, I want to render null
in the view. If the property is not null, I want it to render the property value with a leading and trailing '
.
我该怎么做?
更新:抱歉,稍微改变了问题.
UPDATE: Sorry, changed question slightly.
推荐答案
您应该能够像标题建议的那样使用三元运算符:
You should just be able to use a ternary operator like the title suggests:
@(string.IsNullOrEmpty(Model.Prop) ? "null" : "'" + Model.Prop + "'")
这篇关于ASP.NET MVC Razor:三元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文