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:
parent
f4b582f654
commit
80a27c6f3b
|
|
@ -706,13 +706,11 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
// We don't call ModelMetadata.GetDisplayName here because
|
// We don't call ModelMetadata.GetDisplayName here because
|
||||||
// we want to fall back to the field name rather than the ModelType.
|
// 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.
|
// 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.DisplayName ?? metadata.PropertyName;
|
||||||
var resolvedDisplayName = metadata.PropertyName;
|
|
||||||
if (resolvedDisplayName == null)
|
if (resolvedDisplayName == null)
|
||||||
{
|
{
|
||||||
resolvedDisplayName = string.IsNullOrEmpty(htmlFieldName) ?
|
resolvedDisplayName =
|
||||||
string.Empty :
|
string.IsNullOrEmpty(htmlFieldName) ? string.Empty : htmlFieldName.Split('.').Last();
|
||||||
htmlFieldName.Split('.').Last();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new HtmlString(Encode(resolvedDisplayName));
|
return new HtmlString(Encode(resolvedDisplayName));
|
||||||
|
|
@ -857,13 +855,11 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
string labelText,
|
string labelText,
|
||||||
object htmlAttributes)
|
object htmlAttributes)
|
||||||
{
|
{
|
||||||
// TODO: This needs to be updated after ModelMetadata has a DisplayName property
|
var resolvedLabelText = labelText ?? metadata.DisplayName ?? metadata.PropertyName;
|
||||||
var resolvedLabelText = labelText ?? metadata.PropertyName;
|
|
||||||
if (resolvedLabelText == null)
|
if (resolvedLabelText == null)
|
||||||
{
|
{
|
||||||
resolvedLabelText = string.IsNullOrEmpty(htmlFieldName) ?
|
resolvedLabelText =
|
||||||
string.Empty :
|
string.IsNullOrEmpty(htmlFieldName) ? string.Empty : htmlFieldName.Split('.').Last();
|
||||||
htmlFieldName.Split('.').Last();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(resolvedLabelText))
|
if (string.IsNullOrEmpty(resolvedLabelText))
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,23 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||||
: base.ComputeDescription();
|
: 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()
|
protected override bool ComputeIsReadOnly()
|
||||||
{
|
{
|
||||||
if (PrototypeCache.Editable != null)
|
if (PrototypeCache.Editable != null)
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||||
private bool _convertEmptyStringToNull;
|
private bool _convertEmptyStringToNull;
|
||||||
private string _nullDisplayText;
|
private string _nullDisplayText;
|
||||||
private string _description;
|
private string _description;
|
||||||
|
private string _displayName;
|
||||||
private bool _isReadOnly;
|
private bool _isReadOnly;
|
||||||
private bool _isComplexType;
|
private bool _isComplexType;
|
||||||
private bool _isRequired;
|
private bool _isRequired;
|
||||||
|
|
@ -27,6 +28,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||||
private bool _convertEmptyStringToNullComputed;
|
private bool _convertEmptyStringToNullComputed;
|
||||||
private bool _nullDisplayTextComputed;
|
private bool _nullDisplayTextComputed;
|
||||||
private bool _descriptionComputed;
|
private bool _descriptionComputed;
|
||||||
|
private bool _displayNameComputed;
|
||||||
private bool _isReadOnlyComputed;
|
private bool _isReadOnlyComputed;
|
||||||
private bool _isComplexTypeComputed;
|
private bool _isComplexTypeComputed;
|
||||||
private bool _isRequiredComputed;
|
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
|
public sealed override bool IsReadOnly
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
|
@ -215,6 +236,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||||
return base.Description;
|
return base.Description;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected virtual string ComputeDisplayName()
|
||||||
|
{
|
||||||
|
return base.DisplayName;
|
||||||
|
}
|
||||||
|
|
||||||
protected virtual bool ComputeIsReadOnly()
|
protected virtual bool ComputeIsReadOnly()
|
||||||
{
|
{
|
||||||
return base.IsReadOnly;
|
return base.IsReadOnly;
|
||||||
|
|
|
||||||
|
|
@ -60,6 +60,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||||
|
|
||||||
public virtual string DisplayFormatString { get; set; }
|
public virtual string DisplayFormatString { get; set; }
|
||||||
|
|
||||||
|
public virtual string DisplayName { get; set; }
|
||||||
|
|
||||||
public virtual string EditFormatString { get; set; }
|
public virtual string EditFormatString { get; set; }
|
||||||
|
|
||||||
public virtual bool IsComplexType
|
public virtual bool IsComplexType
|
||||||
|
|
@ -205,7 +207,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||||
|
|
||||||
public string GetDisplayName()
|
public string GetDisplayName()
|
||||||
{
|
{
|
||||||
return PropertyName ?? ModelType.Name;
|
return DisplayName ?? PropertyName ?? ModelType.Name;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual string ComputeSimpleDisplayText()
|
protected virtual string ComputeSimpleDisplayText()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue