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?

Haider Ali Wajihi
  • 2,756
  • 7
  • 51
  • 82
arame3333
  • 9,887
  • 26
  • 122
  • 205
  • 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. – Brian Ogden Sep 03 '14 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. – arame3333 Sep 04 '14 at 06: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 – Brian Ogden Sep 05 '14 at 04:12
  • Possible duplicate of [Why do I get null instead of empty string when receiving POST request in from Razor View?](http://stackoverflow.com/questions/3641723/why-do-i-get-null-instead-of-empty-string-when-receiving-post-request-in-from-ra) – KyleMit Oct 13 '16 at 13:55
  • I asked the question first, so the other question may be a duplicate of mine. – arame3333 Oct 14 '16 at 07:57

4 Answers4

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

Yngve B-Nilsen
  • 9,606
  • 2
  • 36
  • 50
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
djdd87
  • 67,346
  • 27
  • 156
  • 195
  • 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. – arame3333 Aug 13 '10 at 09:11
  • Post some code in your question so we can see what you're tring to do. – djdd87 Aug 13 '10 at 09:22
  • 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; http://stackoverflow.com/questions/1718501/asp-net-mvc-best-way-to-trim-strings-after-data-entry-should-i-create-a-custom 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? – arame3333 Aug 13 '10 at 10:02
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

Community
  • 1
  • 1
Anders
  • 734
  • 8
  • 12
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();