diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/CachedDataAnnotationsModelMetadata.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/CachedDataAnnotationsModelMetadata.cs index d20ec7a2d8..78decd573f 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/CachedDataAnnotationsModelMetadata.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/CachedDataAnnotationsModelMetadata.cs @@ -12,6 +12,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding // is correct. public class CachedDataAnnotationsModelMetadata : CachedModelMetadata { + private static readonly string HtmlName = DataType.Html.ToString(); private bool _isEditFormatStringFromCache; public CachedDataAnnotationsModelMetadata(CachedDataAnnotationsModelMetadata prototype, @@ -47,6 +48,31 @@ namespace Microsoft.AspNet.Mvc.ModelBinding : base.ComputeNullDisplayText(); } + /// + /// Calculate based on presence of a + /// and its method. + /// + /// + /// Calculated value. + /// value if a exists. + /// "Html" if a exists with its + /// value false. null otherwise. + /// + protected override string ComputeDataTypeName() + { + if (PrototypeCache.DataType != null) + { + return PrototypeCache.DataType.GetDataTypeName(); + } + + if (PrototypeCache.DisplayFormat != null && !PrototypeCache.DisplayFormat.HtmlEncode) + { + return HtmlName; + } + + return base.ComputeDataTypeName(); + } + protected override string ComputeDescription() { return PrototypeCache.Display != null diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/CachedModelMetadata.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/CachedModelMetadata.cs index 76b992de94..171b137bd9 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/CachedModelMetadata.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/CachedModelMetadata.cs @@ -17,6 +17,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding { private bool _convertEmptyStringToNull; private string _nullDisplayText; + private string _dataTypeName; private string _description; private string _displayFormatString; private string _displayName; @@ -31,6 +32,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding private bool _convertEmptyStringToNullComputed; private bool _nullDisplayTextComputed; + private bool _dataTypeNameComputed; private bool _descriptionComputed; private bool _displayFormatStringComputed; private bool _displayNameComputed; @@ -105,6 +107,27 @@ namespace Microsoft.AspNet.Mvc.ModelBinding } } + /// + public sealed override string DataTypeName + { + get + { + if (!_dataTypeNameComputed) + { + _dataTypeName = ComputeDataTypeName(); + _dataTypeNameComputed = true; + } + + return _dataTypeName; + } + + set + { + _dataTypeName = value; + _dataTypeNameComputed = true; + } + } + public sealed override string Description { get @@ -338,6 +361,15 @@ namespace Microsoft.AspNet.Mvc.ModelBinding return base.NullDisplayText; } + /// + /// Calculate the value. + /// + /// Calculated value. + protected virtual string ComputeDataTypeName() + { + return base.DataTypeName; + } + protected virtual string ComputeDescription() { return base.Description; diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/ModelMetadata.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/ModelMetadata.cs index db1dda5083..7faf1015c1 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/ModelMetadata.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/ModelMetadata.cs @@ -59,6 +59,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding set { _convertEmptyStringToNull = value; } } + /// + /// Gets or sets the name of the 's datatype. Overrides in some + /// display scenarios. + /// + /// null unless set manually or through additional metadata e.g. attributes. public virtual string DataTypeName { get; set; } public virtual string Description { get; set; } diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Metadata/CachedDataAnnotationsModelMetadataTest.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Metadata/CachedDataAnnotationsModelMetadataTest.cs index 1f2149d41d..31443607b0 100644 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Metadata/CachedDataAnnotationsModelMetadataTest.cs +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Metadata/CachedDataAnnotationsModelMetadataTest.cs @@ -56,6 +56,9 @@ namespace Microsoft.AspNet.Mvc.ModelBinding { return new TheoryData> { + { + new DataTypeAttribute("value"), metadata => metadata.DataTypeName + }, { new DataTypeWithCustomDisplayFormat(), metadata => metadata.DisplayFormatString }, @@ -207,6 +210,69 @@ namespace Microsoft.AspNet.Mvc.ModelBinding Assert.Equal(expectedResult, result); } + [Fact] + public void DataTypeName_Null_IfHtmlEncodeTrue() + { + // Arrange + var displayFormat = new DisplayFormatAttribute { HtmlEncode = true, }; + var provider = new DataAnnotationsModelMetadataProvider(); + var metadata = new CachedDataAnnotationsModelMetadata( + provider, + containerType: null, + modelType: typeof(object), + propertyName: null, + attributes: new Attribute[] { displayFormat }); + + // Act + var result = metadata.DataTypeName; + + // Assert + Assert.Null(result); + } + + [Fact] + public void DataTypeName_Html_IfHtmlEncodeFalse() + { + // Arrange + var expected = "Html"; + var displayFormat = new DisplayFormatAttribute { HtmlEncode = false, }; + var provider = new DataAnnotationsModelMetadataProvider(); + var metadata = new CachedDataAnnotationsModelMetadata( + provider, + containerType: null, + modelType: typeof(object), + propertyName: null, + attributes: new Attribute[] { displayFormat }); + + // Act + var result = metadata.DataTypeName; + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void DataTypeName_AttributesHaveExpectedPrecedence() + { + // Arrange + var expected = "MultilineText"; + var dataType = new DataTypeAttribute(DataType.MultilineText); + var displayFormat = new DisplayFormatAttribute { HtmlEncode = false, }; + var provider = new DataAnnotationsModelMetadataProvider(); + var metadata = new CachedDataAnnotationsModelMetadata( + provider, + containerType: null, + modelType: typeof(object), + propertyName: null, + attributes: new Attribute[] { dataType, displayFormat }); + + // Act + var result = metadata.DataTypeName; + + // Assert + Assert.Equal(expected, result); + } + [Fact] public void DisplayFormatString_AttributesHaveExpectedPrecedence() { @@ -220,7 +286,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding containerType: null, modelType: typeof(object), propertyName: null, - attributes: new Attribute[] { dataType, displayFormat, }); + attributes: new Attribute[] { dataType, displayFormat }); // Act var result = metadata.DisplayFormatString; @@ -246,7 +312,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding containerType: null, modelType: typeof(object), propertyName: null, - attributes: new Attribute[] { dataType, displayFormat, }); + attributes: new Attribute[] { dataType, displayFormat }); // Act var result = metadata.EditFormatString;