Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If value is null put an empty string on razor template?

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?

Normal Input:

<input name="EMail" id="SignUpEMail" type="text" class="Input"         value="@UIManager.Member.EMail" validate="RequiredField" /> 

Razor Syntax Attempt:

<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 
like image 409
Barış Velioğlu Avatar asked Aug 17 '11 08:08

Barış Velioğlu


People also ask

Can a string be null and empty?

The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters.

Is it better to use null or empty string?

So, NULL is better. An empty string is useful when the data comes from multiple resources. NULL is used when some fields are optional, and the data is unknown.

Is empty string nil?

An empty string is a String object with an assigned value, but its length is equal to zero. A null string has no value at all.

How do you check is null or empty in Cshtml?

If you have the type DateTime? (this means nullable) and set it to nothing, you get a null value...


1 Answers

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

like image 148
Marc Gravell Avatar answered Sep 16 '22 13:09

Marc Gravell