diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs index d75be99e78..0a15584962 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs @@ -706,13 +706,11 @@ namespace Microsoft.AspNet.Mvc.Rendering // We don't call ModelMetadata.GetDisplayName here because // we want to fall back to the field name rather than the ModelType. // This is similar to how the GenerateLabel get the text of a label. - // TODO: This needs to be updated after ModelMetadata has a DisplayName property - var resolvedDisplayName = metadata.PropertyName; + var resolvedDisplayName = metadata.DisplayName ?? metadata.PropertyName; if (resolvedDisplayName == null) { - resolvedDisplayName = string.IsNullOrEmpty(htmlFieldName) ? - string.Empty : - htmlFieldName.Split('.').Last(); + resolvedDisplayName = + string.IsNullOrEmpty(htmlFieldName) ? string.Empty : htmlFieldName.Split('.').Last(); } return new HtmlString(Encode(resolvedDisplayName)); @@ -857,13 +855,11 @@ namespace Microsoft.AspNet.Mvc.Rendering string labelText, object htmlAttributes) { - // TODO: This needs to be updated after ModelMetadata has a DisplayName property - var resolvedLabelText = labelText ?? metadata.PropertyName; + var resolvedLabelText = labelText ?? metadata.DisplayName ?? metadata.PropertyName; if (resolvedLabelText == null) { - resolvedLabelText = string.IsNullOrEmpty(htmlFieldName) ? - string.Empty : - htmlFieldName.Split('.').Last(); + resolvedLabelText = + string.IsNullOrEmpty(htmlFieldName) ? string.Empty : htmlFieldName.Split('.').Last(); } if (string.IsNullOrEmpty(resolvedLabelText)) diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/CachedDataAnnotationsModelMetadata.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/CachedDataAnnotationsModelMetadata.cs index fe0a00c7d9..8b6421b4e4 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/CachedDataAnnotationsModelMetadata.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/CachedDataAnnotationsModelMetadata.cs @@ -50,6 +50,23 @@ namespace Microsoft.AspNet.Mvc.ModelBinding : base.ComputeDescription(); } + protected override string ComputeDisplayName() + { + // DisplayName may be provided by DisplayAttribute. + // If that does not supply a name, then we fall back to the property name (in base.GetDisplayName()). + if (PrototypeCache.Display != null) + { + // DisplayAttribute doesn't require you to set a name, so this could be null. + var name = PrototypeCache.Display.GetName(); + if (name != null) + { + return name; + } + } + + return base.ComputeDisplayName(); + } + protected override bool ComputeIsReadOnly() { if (PrototypeCache.Editable != null) diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/CachedModelMetadata.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/CachedModelMetadata.cs index 4b0aea6c82..15b1acf53d 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/CachedModelMetadata.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/CachedModelMetadata.cs @@ -18,6 +18,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding private bool _convertEmptyStringToNull; private string _nullDisplayText; private string _description; + private string _displayName; private bool _isReadOnly; private bool _isComplexType; private bool _isRequired; @@ -27,6 +28,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding private bool _convertEmptyStringToNullComputed; private bool _nullDisplayTextComputed; private bool _descriptionComputed; + private bool _displayNameComputed; private bool _isReadOnlyComputed; private bool _isComplexTypeComputed; private bool _isRequiredComputed; @@ -113,6 +115,25 @@ namespace Microsoft.AspNet.Mvc.ModelBinding } } + public sealed override string DisplayName + { + get + { + if (!_displayNameComputed) + { + _displayName = ComputeDisplayName(); + _displayNameComputed = true; + } + + return _displayName; + } + set + { + _displayName = value; + _displayNameComputed = true; + } + } + public sealed override bool IsReadOnly { get @@ -215,6 +236,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding return base.Description; } + protected virtual string ComputeDisplayName() + { + return base.DisplayName; + } + protected virtual bool ComputeIsReadOnly() { return base.IsReadOnly; diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/ModelMetadata.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/ModelMetadata.cs index 8c79d131c9..1d533d2fdb 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/ModelMetadata.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/ModelMetadata.cs @@ -60,6 +60,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding public virtual string DisplayFormatString { get; set; } + public virtual string DisplayName { get; set; } + public virtual string EditFormatString { get; set; } public virtual bool IsComplexType @@ -205,7 +207,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding public string GetDisplayName() { - return PropertyName ?? ModelType.Name; + return DisplayName ?? PropertyName ?? ModelType.Name; } protected virtual string ComputeSimpleDisplayText()