30

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?

5
  • I have always found avoiding nulls as much as possible a strange thing. Null is a beautiful thing. In a database table record row has a nullable column and it is null it means no value as ever been provided before. This knowledge is so important to business rules and logics for companies I have found time and time again. Commented Sep 3, 2014 at 17:58
  • I can see nulls are useful when calculating an average for a range of numbers. Also null is good for dates that have not been entered yet. Otherwise why is a null better than an empty string or a zero? All it seems to do is generate more boiler plate code to stop unwanted nullreference exceptions. Commented Sep 4, 2014 at 6:21
  • That is a good point, although instead of checking for null at most of the same places, to prevent unwanted null reference exceptions, you are checking for if!EmptyString). The null reference exceptions get old but they might be a good level of "quality control/testing". Empty string passes, no exception, but it shouldn't be empty, null fires an exception and thus a developer discovers bugs faster perhaps. Its funny, I hate seeing empty string columns in Sql Select results lol Commented Sep 5, 2014 at 4:12
  • Possible duplicate of Why do I get null instead of empty string when receiving POST request in from Razor View? Commented Oct 13, 2016 at 13:55
  • I asked the question first, so the other question may be a duplicate of mine. Commented Oct 14, 2016 at 7:57

4 Answers 4

62

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

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

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.
Post some code in your question so we can see what you're tring to do.
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?
1

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

Comments

1

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();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.