如果value为null,则在剃刀模板上放一个空字符串? [英] If value is null put an empty string on razor template?
本文介绍了如果value为null,则在剃刀模板上放一个空字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下的剃刀模板.我想检查输入字段中的值是否为空,放置一个空字符串,如果@UIManager.Member.EMail
有一个值,则放置它的值.我该怎么办?
I have a razor template like below. I want to check if the value in the input field is null, put a empty string, if the @UIManager.Member.EMail
has a value, put its value. How can I do that?
正常输入:
<input name="EMail" id="SignUpEMail" type="text" class="Input"
value="@UIManager.Member.EMail" validate="RequiredField" />
剃刀语法尝试:
<input name="EMail" id="SignUpEMail" type="text" class="Input" validate="RequiredField"
value="@(UIManager.Member == null) ? string.Empty : UIManager.Member.EMail" />
在输入字段中显示的值是:
The value is shown in the input field is:
True ? string.Empty : UIBusinessManager.MemberCandidate.EMail
推荐答案
如果您只想听这样的话:
If sounds like you just want:
@(UIManager.Member == null ? "" : UIManager.Member.Email)
请注意,括号的位置很重要;使用剃刀,@(....)
为代码定义了一个显式范围-因此,括号内 之外的所有内容均被视为标记(不是代码).
Note the locations of the brackets is critical; with razor, @(....)
defines an explicit range to the code - hence anything outside the brackets is treated as markup (not code).
这篇关于如果value为null,则在剃刀模板上放一个空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文