EntityFramework-Reverse-POCO-Code-First-Generator icon indicating copy to clipboard operation
EntityFramework-Reverse-POCO-Code-First-Generator copied to clipboard

Model Binding string to null

Open mmisnerms opened this issue 4 years ago • 1 comments

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]");
        }

mmisnerms avatar May 28 '21 11:05 mmisnerms

Thanks @mmisnerms I'll have a look in a few days when I get back.

sjh37 avatar May 28 '21 23:05 sjh37