From 48085eea086bd2032f6b2109c091b265e6febe75 Mon Sep 17 00:00:00 2001 From: dougbu Date: Fri, 13 Jun 2014 12:36:41 -0700 Subject: [PATCH] Correct `ModelMetadata.IsRequired` to honour overrides - subclasses would not calculate a value because `ModelMetadata` constructor set this property - meant presence of a `[Required]` attribute in the model was ignored --- .../Metadata/ModelMetadata.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/ModelMetadata.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/ModelMetadata.cs index 1d533d2fdb..2b44574291 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/ModelMetadata.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/ModelMetadata.cs @@ -18,7 +18,10 @@ namespace Microsoft.AspNet.Mvc.ModelBinding private readonly string _propertyName; private EfficientTypePropertyKey _cacheKey; - private bool _convertEmptyStringToNull = true; + // Backing fields for virtual properties with default values. + private bool _convertEmptyStringToNull; + private bool _isRequired; + private object _model; private Func _modelAccessor; private int _order = DefaultOrder; @@ -40,7 +43,9 @@ namespace Microsoft.AspNet.Mvc.ModelBinding _modelAccessor = modelAccessor; _modelType = modelType; _propertyName = propertyName; - IsRequired = !modelType.AllowsNullValue(); + + _convertEmptyStringToNull = true; + _isRequired = !modelType.AllowsNullValue(); } public Type ContainerType @@ -76,7 +81,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding public virtual bool IsReadOnly { get; set; } - public virtual bool IsRequired { get; set; } + public virtual bool IsRequired + { + get { return _isRequired; } + set { _isRequired = value; } + } public virtual int Order {