The attribute, DisplayFormatAttribute.ConvertEmptyStringToNull has a default of true. I would like to default it to false for the entire site (or by class or page would be good too). Is there a way I can do this so I don't need to decorate each test form field with:
[DisplayFormat(ConvertEmptyStringToNull=false)]
You can create your own custom model metadata provider like this:
public class CustomModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
protected override ModelMetadata CreateMetadata(IEnumerable<System.Attribute> attributes, System.Type containerType, System.Func<object> modelAccessor, System.Type modelType, string propertyName)
{
var modelMetadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
if (string.IsNullOrEmpty(propertyName)) return modelMetadata;
if (modelType == typeof(String))
modelMetadata.ConvertEmptyStringToNull = false;
return modelMetadata;
}
}
Then register it in your app_start:
ModelMetadataProviders.Current = new CustomModelMetadataProvider();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With