Support `[Display(Name = "")]` on enum values

- #2851
This commit is contained in:
Doug Bunting 2015-08-06 10:37:50 -07:00
parent 4a813773d0
commit ed4896d1fd
2 changed files with 7 additions and 5 deletions

View File

@ -75,7 +75,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
displayMetadata.Description = () => displayAttribute.GetDescription();
}
// DisplayFormat
// DisplayFormatString
if (displayFormatAttribute != null)
{
displayMetadata.DisplayFormatString = displayFormatAttribute.DataFormatString;
@ -87,6 +87,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
displayMetadata.DisplayName = () => displayAttribute.GetName();
}
// EditFormatString
if (displayFormatAttribute != null && displayFormatAttribute.ApplyFormatInEditMode)
{
displayMetadata.EditFormatString = displayFormatAttribute.DataFormatString;
@ -233,8 +234,9 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
var display = field.GetCustomAttribute<DisplayAttribute>(inherit: false);
if (display != null)
{
// Note [Display(Name = "")] is allowed.
var name = display.GetName();
if (!string.IsNullOrEmpty(name))
if (name != null)
{
return name;
}

View File

@ -424,7 +424,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
typeof(EnumWithDisplayNames),
new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("cero", "0"),
new KeyValuePair<string, string>(string.Empty, "0"),
new KeyValuePair<string, string>(nameof(EnumWithDisplayNames.One), "1"),
new KeyValuePair<string, string>("dos", "2"),
new KeyValuePair<string, string>("tres", "3"),
@ -436,7 +436,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
typeof(EnumWithDisplayNames?),
new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("cero", "0"),
new KeyValuePair<string, string>(string.Empty, "0"),
new KeyValuePair<string, string>(nameof(EnumWithDisplayNames.One), "1"),
new KeyValuePair<string, string>("dos", "2"),
new KeyValuePair<string, string>("tres", "3"),
@ -708,7 +708,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
[Display(ShortName = "uno")]
One = 1,
[Display(Name = "cero")]
[Display(Name = "")]
Zero = 0,
[Display(Name = "menos uno")]