Add `ModelMetadata.DisplayName` property

- value comes from `Name` property of `[Display]` attribute
- use new property in `@Html.DisplayName()` and `@Html.Label()`; remove associated TODO comments
This commit is contained in:
dougbu 2014-06-13 12:41:22 -07:00
parent f4b582f654
commit 80a27c6f3b
4 changed files with 52 additions and 11 deletions

View File

@ -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))

View File

@ -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)

View File

@ -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;

View File

@ -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()