How to set default value of TextBox empty string instead of null

23,203

Solution 1

You could put the following attribute on your string-properties in your model:

[DisplayFormat(ConvertEmptyStringToNull=false)]

So whenever someone posts a form with empty text-fields, these will be an empty string instead of null...

Solution 2

To be honest, I'd say your coding methodology is out of date and flawed. You should handle all possibilities, it's not hard. That's exactly what string.IsNullOrEmpty(value); is for.

I'm guessing your validation logic is something like:

if (value == string.Empty) { isValid = false; } 

So it doesn't handle the null values. You should replace that check so it also checks for nulls.

string value1 = null;
string value2 = string.Empty;

string.IsNullOrEmpty(value1); // true
string.IsNullOrEmpty(value2); // true

Solution 3

An alternative solution to using attributes on each model property, as described in the accepted answer, is using a custom model binder, see string.empty converted to null when passing JSON object to MVC Controller

Solution 4

I ran across this problem when dealing with an old service that requires empty strings. I created an extension method:

public static string GetValueOrDefault(this string str)
        {
            return str ?? String.Empty;
        }

So you can use this when you want to make sure any strings that are null become empty

yourString1.GetValueOrDefault();
yourString2.GetValueOrDefault();
Share:
23,203
arame3333
Author by

arame3333

Updated on July 19, 2022

Comments

  • arame3333
    arame3333 almost 2 years

    I may be out of date, but one principle I adhere to is avoid nulls as much as possible.

    However what I have found is that for a strongly typed view in which the user inputs the properties of an object I want to save, if some fields are not entered they are assigned as null.

    Then when you try to save the changes, the validation fails.

    So rather than set each property to an empty string, how can I automatically set each TextBox on a form to default to an empty string rather than a null?

  • arame3333
    arame3333 almost 14 years
    I can see my question isn't clear. I am looking for an answer to do with metadata or an extended template. This is a MVC question rather than a C# question.
  • djdd87
    djdd87 almost 14 years
    Post some code in your question so we can see what you're tring to do.
  • arame3333
    arame3333 almost 14 years
    I am not sure what code I can show you. I have some TextBoxes in a View, and if the user does not enter a value before submitting, they default to null. If the fields in the underlying model are NOT nullable, this causes the validation to fail. I have found an answer based on this question on SO; stackoverflow.com/questions/1718501/… Instead of trimming I just set a null string to an empty string. I am happy with this answer, but maybe there is an easier one?
  • Haider Ali Wajihi
    Haider Ali Wajihi about 10 years
    it is that, what i am looking for. Thanx @Yngve B. Nilsen