EntityFramework-Reverse-POCO-Code-First-Generator
EntityFramework-Reverse-POCO-Code-First-Generator copied to clipboard
Model Binding string to null
When model binding a string with empty string the value is set to null since string is a reference type.
When the column is set to not null/required the ModelState is invalid.
Would it make sense to change that to empty string if the column is set to not null?
I tested this by adding ConvertEmptyStringToNull to the following tt code.
if (!column.IsNullable && !column.IsComputed)
{
if (column.PropertyType.Equals("string", StringComparison.InvariantCultureIgnoreCase) && column.AllowEmptyStrings)
{
column.Attributes.Add("[Required(AllowEmptyStrings = true)]");
column.Attributes.Add("[DisplayFormat(ConvertEmptyStringToNull = false)]");
}
else
column.Attributes.Add("[Required]");
}
Thanks @mmisnerms I'll have a look in a few days when I get back.