Null strings in ASP.NET MVC 1.0 with DataAnnotations
After getting DataAnnotations working with ASP.NET MVC 1.0, I noticed a strange anomaly. Values that were previously bound as empty strings were now being bound as null values.
The reasoning behind this is that the DefaultBinder was changed in the ApplicationStart to use the new fangled, and unsupported Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder!
This ModelBinder has different default behavior. The clue was found in looking through the source code for the DataAnnotationsModelBinder as shown below:
/// Converts a data value. Looks for the <see cref="DisplayFormatAttribute"/> to determine
/// if empty strings should be converted to nulls (true by default).
/// </summary>
/// <param name="propertyDescriptor">The property's property descriptor</param>
/// <param name="value">The original value</param>
/// <returns>The converted value</returns>
internal static object ConvertValue(PropertyDescriptor propertyDescriptor, object value)
{
var displayFormatAttribute = propertyDescriptor.GetAttribute<DisplayFormatAttribute>();
if ((displayFormatAttribute == null || displayFormatAttribute.ConvertEmptyStringToNull) && Object.Equals(value, String.Empty)) {
return null;
}
return value;
}
It says it all right there. The default behavior for empty strings is to set the property to null! In order to change this default behavior, you must use the DisplayFormat attribute and set the ConvertEmptyStringToNull named parameter to false as shown below for the MiddleName property.
{
[Required]
public object FirstName { get; set; }
[DisplayFormat(ConvertEmptyStringToNull = false)]
public object MiddleName { get; set; }
[Required]
public object LastName { get; set; }
}
<code>
Now the MiddleName can be left blank by the user and it will be stored in the datastore as an empty string.
Recent blog posts
- Canned VirtualPC Instances for IE 6, 7, 8 on XP/Vista
- Checking assembly dependencies for .NET
- Google's Public DNS
- Server Utility Functions for Non-Web Apps
- reCAPTCHA for ASP.NET MVC that uses ModelState
- Adding a container to ValidationSummary helper in ASP.NET MVC
- Generic XML Serialization Class
- Re-throwing Exceptions in C# with InternalPreserveStackTrace
- Solving xsd generation error: 'The element .... is missing'
- Enum DropDownList in ASP.NET MVC
Recent comments
- so very helpful kenny. thank
9 weeks 2 days ago - Sorry, apparently the drupal
12 weeks 3 days ago - A better method
15 weeks 4 days ago - No Source Code?
21 weeks 2 days ago - security
31 weeks 1 day ago - Nice simple solution
1 year 15 weeks ago - That's quite an interesting
1 year 32 weeks ago - Small Complaint
1 year 33 weeks ago - NIceeee
1 year 39 weeks ago - Both the SQL Server connector
2 years 16 weeks ago
Comments
#1 Anonymous
Blank = null in a lot of databases.
#2 bmancini
Both the SQL Server connector and MySQL connector treat string.Empty as an empty string and null or DBNULL.Value as null database values.
If your table has a column marked as not null and receives a null value it will throw an exception. This is what happened to me.
Basically, the DataAnnotations model binder changed default behavior for handling of empty values. This post shows you how to override the default behavior so it works how you want it to.