diff --git a/src/Microsoft.AspNetCore.Mvc.Abstractions/ModelBinding/ModelMetadata.cs b/src/Microsoft.AspNetCore.Mvc.Abstractions/ModelBinding/ModelMetadata.cs
index be2d6cebc8..7de72be66b 100644
--- a/src/Microsoft.AspNetCore.Mvc.Abstractions/ModelBinding/ModelMetadata.cs
+++ b/src/Microsoft.AspNetCore.Mvc.Abstractions/ModelBinding/ModelMetadata.cs
@@ -92,8 +92,8 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
public abstract BindingSource BindingSource { get; }
///
- /// Gets a value indicating whether or not to convert an empty string value to null when
- /// representing a model as text.
+ /// Gets a value indicating whether or not to convert an empty string value or one containing only whitespace
+ /// characters to null when representing a model as text.
///
public abstract bool ConvertEmptyStringToNull { get; }
diff --git a/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Binders/SimpleTypeModelBinder.cs b/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Binders/SimpleTypeModelBinder.cs
index ad70420966..92ab83ad2f 100644
--- a/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Binders/SimpleTypeModelBinder.cs
+++ b/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Binders/SimpleTypeModelBinder.cs
@@ -47,8 +47,25 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
{
var value = valueProviderResult.FirstValue;
- object model = null;
- if (!string.IsNullOrWhiteSpace(value))
+ object model;
+ if (bindingContext.ModelType == typeof(string))
+ {
+ // Already have a string. No further conversion required but handle ConvertEmptyStringToNull.
+ if (bindingContext.ModelMetadata.ConvertEmptyStringToNull && string.IsNullOrWhiteSpace(value))
+ {
+ model = null;
+ }
+ else
+ {
+ model = value;
+ }
+ }
+ else if (string.IsNullOrWhiteSpace(value))
+ {
+ // Other than the StringConverter, converters Trim() the value then throw if the result is empty.
+ model = null;
+ }
+ else
{
model = _typeConverter.ConvertFrom(
context: null,
@@ -56,16 +73,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
value: value);
}
- if (bindingContext.ModelType == typeof(string))
- {
- var modelAsString = model as string;
- if (bindingContext.ModelMetadata.ConvertEmptyStringToNull &&
- string.IsNullOrEmpty(modelAsString))
- {
- model = null;
- }
- }
-
// When converting newModel a null value may indicate a failed conversion for an otherwise required
// model (can't set a ValueType to null). This detects if a null model value is acceptable given the
// current bindingContext. If not, an error is logged.
@@ -75,7 +82,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
bindingContext.ModelName,
bindingContext.ModelMetadata.ModelBindingMessageProvider.ValueMustNotBeNullAccessor(
valueProviderResult.ToString()));
-
+
return TaskCache.CompletedTask;
}
else
diff --git a/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Metadata/DisplayMetadata.cs b/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Metadata/DisplayMetadata.cs
index 4d02a152e8..f1fd3fef08 100644
--- a/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Metadata/DisplayMetadata.cs
+++ b/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Metadata/DisplayMetadata.cs
@@ -17,8 +17,9 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata
public IDictionary