parent
d2b7cc9de2
commit
4f3e044928
|
|
@ -2,6 +2,7 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
|
||||
|
|
@ -40,6 +41,22 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
|
|||
return modelExplorer.Metadata.NullDisplayText;
|
||||
}
|
||||
|
||||
if (modelExplorer.Metadata.IsEnum && modelExplorer.Model is Enum modelEnum)
|
||||
{
|
||||
var enumStringValue = modelEnum.ToString("d");
|
||||
var enumGroupedDisplayNamesAndValues = modelExplorer.Metadata.EnumGroupedDisplayNamesAndValues;
|
||||
|
||||
Debug.Assert(enumGroupedDisplayNamesAndValues != null);
|
||||
|
||||
foreach (var kvp in enumGroupedDisplayNamesAndValues)
|
||||
{
|
||||
if (string.Equals(kvp.Value, enumStringValue, StringComparison.Ordinal))
|
||||
{
|
||||
return kvp.Key.Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var stringResult = Convert.ToString(modelExplorer.Model, CultureInfo.CurrentCulture);
|
||||
if (stringResult == null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Xunit;
|
||||
|
||||
|
|
@ -296,6 +297,34 @@ namespace Microsoft.AspNetCore.Mvc.Rendering
|
|||
Assert.Equal("Property value", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DisplayTextFor_EnumDisplayAttribute_WhenPresent()
|
||||
{
|
||||
// Arrange
|
||||
var model = EnumWithDisplayAttribute.Value1;
|
||||
var helper = DefaultTemplatesUtilities.GetHtmlHelper(model);
|
||||
|
||||
// Act
|
||||
var result = helper.DisplayTextFor(m => m);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("Value One", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DisplayTextFor_EnumDisplayAttribute_WhenNotPresent()
|
||||
{
|
||||
// Arrange
|
||||
var model = EnumWithoutDisplayAttribute.Value1;
|
||||
var helper = DefaultTemplatesUtilities.GetHtmlHelper(model);
|
||||
|
||||
// Act
|
||||
var result = helper.DisplayTextFor(m => m);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("Value1", result);
|
||||
}
|
||||
|
||||
// ModelMetadata.SimpleDisplayText returns ToString() result if that method has been overridden.
|
||||
private sealed class OverriddenToStringModel
|
||||
{
|
||||
|
|
@ -315,5 +344,16 @@ namespace Microsoft.AspNetCore.Mvc.Rendering
|
|||
return _simpleDisplayText;
|
||||
}
|
||||
}
|
||||
|
||||
public enum EnumWithDisplayAttribute
|
||||
{
|
||||
[Display(Name = "Value One")]
|
||||
Value1
|
||||
}
|
||||
|
||||
public enum EnumWithoutDisplayAttribute
|
||||
{
|
||||
Value1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue