Making the HtmlHelper.GetEnumSelectList take DisplayAttribute.GroupName into account to create select groups.
This commit is contained in:
parent
9243d832de
commit
c713aa92ca
|
|
@ -0,0 +1,21 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MvcSample.Web.Models
|
||||||
|
{
|
||||||
|
public enum TestEnum
|
||||||
|
{
|
||||||
|
Zero = 0,
|
||||||
|
[Display(GroupName = "Primes")]
|
||||||
|
One = 1,
|
||||||
|
[Display(GroupName = "Evens", Name = "Dos")]
|
||||||
|
Two = 2,
|
||||||
|
[Display(GroupName = "Primes")]
|
||||||
|
Three = 3,
|
||||||
|
[Display(Name = "4th")]
|
||||||
|
Four = 4
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -29,6 +29,7 @@ namespace MvcSample.Web.Models
|
||||||
public string About { get; set; }
|
public string About { get; set; }
|
||||||
public string Log { get; set; }
|
public string Log { get; set; }
|
||||||
public IEnumerable<string> OwnedAddresses { get; private set; }
|
public IEnumerable<string> OwnedAddresses { get; private set; }
|
||||||
|
public TestEnum EnumInformation { get; set; }
|
||||||
|
|
||||||
// This does not bind correctly. Only gets highest value.
|
// This does not bind correctly. Only gets highest value.
|
||||||
public List<int> ParentsAges { get; private set; }
|
public List<int> ParentsAges { get; private set; }
|
||||||
|
|
|
||||||
|
|
@ -105,12 +105,22 @@
|
||||||
@Html.ListBoxFor(model => model.ParentsAges, (IEnumerable<SelectListItem>)ViewBag.Ages, htmlAttributes: new { @class = "form-control" })
|
@Html.ListBoxFor(model => model.ParentsAges, (IEnumerable<SelectListItem>)ViewBag.Ages, htmlAttributes: new { @class = "form-control" })
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<label class="control-label col-md-2" for="Name">Model.EnumInformation</label>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
@Html.DropDownList("EnumInformation", Html.GetEnumSelectList<TestEnum>())
|
||||||
|
</td>
|
||||||
|
<td></td>
|
||||||
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<input type="submit" value="Save" class="btn btn-default" style="margin-left: 10px" />
|
<input type="submit" value="Save" class="btn btn-default" style="margin-left: 10px" />
|
||||||
</td>
|
</td>
|
||||||
<td></td>
|
<td></td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
</table>
|
</table>
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,44 @@
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// An abstraction used when grouping enum values for <see cref="ModelMetadata.EnumGroupedDisplayNamesAndValues"/>.
|
||||||
|
/// </summary>
|
||||||
|
public struct EnumGroupAndName
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the EnumGroupAndName structure.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="group">The group name.</param>
|
||||||
|
/// <param name="name">The name.</param>
|
||||||
|
public EnumGroupAndName(string group, string name)
|
||||||
|
{
|
||||||
|
if (group == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(group));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (name == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
Group = group;
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the Group name.
|
||||||
|
/// </summary>
|
||||||
|
public string Group { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the name.
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -139,14 +139,14 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||||
public abstract ModelMetadata ElementMetadata { get; }
|
public abstract ModelMetadata ElementMetadata { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the ordered display names and values of all <see cref="Enum"/> values in
|
/// Gets the ordered and grouped display names and values of all <see cref="Enum"/> values in
|
||||||
/// <see cref="UnderlyingOrModelType"/>.
|
/// <see cref="UnderlyingOrModelType"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>
|
/// <value>
|
||||||
/// An <see cref="IEnumerable{KeyValuePair{string, string}}"/> of mappings between <see cref="Enum"/> field names
|
/// An <see cref="IEnumerable{KeyValuePair{EnumGroupAndName, string}"/> of mappings between
|
||||||
/// and values. <c>null</c> if <see cref="IsEnum"/> is <c>false</c>.
|
/// <see cref="Enum"/> field groups, names and values. <c>null</c> if <see cref="IsEnum"/> is <c>false</c>.
|
||||||
/// </value>
|
/// </value>
|
||||||
public abstract IEnumerable<KeyValuePair<string, string>> EnumDisplayNamesAndValues { get; }
|
public abstract IEnumerable<KeyValuePair<EnumGroupAndName, string>> EnumGroupedDisplayNamesAndValues { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the names and values of all <see cref="Enum"/> values in <see cref="UnderlyingOrModelType"/>.
|
/// Gets the names and values of all <see cref="Enum"/> values in <see cref="UnderlyingOrModelType"/>.
|
||||||
|
|
|
||||||
|
|
@ -272,11 +272,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override IEnumerable<KeyValuePair<string, string>> EnumDisplayNamesAndValues
|
public override IEnumerable<KeyValuePair<EnumGroupAndName, string>> EnumGroupedDisplayNamesAndValues
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return DisplayMetadata.EnumDisplayNamesAndValues;
|
return DisplayMetadata.EnumGroupedDisplayNamesAndValues;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -57,11 +57,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
||||||
public string EditFormatString { get; set; }
|
public string EditFormatString { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the ordered display names and values of all <see cref="System.Enum"/> values in
|
/// Gets the ordered and grouped display names and values of all <see cref="System.Enum"/> values in
|
||||||
/// <see cref="ModelMetadata.UnderlyingOrModelType"/>. See
|
/// <see cref="ModelMetadata.UnderlyingOrModelType"/>. See
|
||||||
/// <see cref="ModelMetadata.EnumDisplayNamesAndValues"/>.
|
/// <see cref="ModelMetadata.EnumGroupedDisplayNamesAndValues"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IEnumerable<KeyValuePair<string, string>> EnumDisplayNamesAndValues { get; set; }
|
public IEnumerable<KeyValuePair<EnumGroupAndName, string>> EnumGroupedDisplayNamesAndValues { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the names and values of all <see cref="System.Enum"/> values in
|
/// Gets the names and values of all <see cref="System.Enum"/> values in
|
||||||
|
|
|
||||||
|
|
@ -120,19 +120,20 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
||||||
// Order EnumDisplayNamesAndValues to match Enum.GetNames(). That method orders by absolute value,
|
// Order EnumDisplayNamesAndValues to match Enum.GetNames(). That method orders by absolute value,
|
||||||
// then its behavior is undefined (but hopefully stable). Add to EnumNamesAndValues in same order but
|
// then its behavior is undefined (but hopefully stable). Add to EnumNamesAndValues in same order but
|
||||||
// Dictionary does not guarantee order will be preserved.
|
// Dictionary does not guarantee order will be preserved.
|
||||||
var displayNamesAndValues = new List<KeyValuePair<string, string>>();
|
var groupedDisplayNamesAndValues = new List<KeyValuePair<EnumGroupAndName, string>>();
|
||||||
var namesAndValues = new Dictionary<string, string>();
|
var namesAndValues = new Dictionary<string, string>();
|
||||||
foreach (var name in Enum.GetNames(underlyingType))
|
foreach (var name in Enum.GetNames(underlyingType))
|
||||||
{
|
{
|
||||||
var field = underlyingType.GetField(name);
|
var field = underlyingType.GetField(name);
|
||||||
var displayName = GetDisplayName(field);
|
var displayName = GetDisplayName(field);
|
||||||
|
var groupName = GetDisplayGroup(field);
|
||||||
var value = ((Enum)field.GetValue(obj: null)).ToString("d");
|
var value = ((Enum)field.GetValue(obj: null)).ToString("d");
|
||||||
|
|
||||||
displayNamesAndValues.Add(new KeyValuePair<string, string>(displayName, value));
|
groupedDisplayNamesAndValues.Add(new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(groupName, displayName), value));
|
||||||
namesAndValues.Add(name, value);
|
namesAndValues.Add(name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
displayMetadata.EnumDisplayNamesAndValues = displayNamesAndValues;
|
displayMetadata.EnumGroupedDisplayNamesAndValues = groupedDisplayNamesAndValues;
|
||||||
displayMetadata.EnumNamesAndValues = namesAndValues;
|
displayMetadata.EnumNamesAndValues = namesAndValues;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -258,5 +259,22 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
||||||
|
|
||||||
return field.Name;
|
return field.Name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return non-empty group specified in a [Display] attribute for a field, if any; string.Empty otherwise.
|
||||||
|
private static string GetDisplayGroup(FieldInfo field)
|
||||||
|
{
|
||||||
|
var display = field.GetCustomAttribute<DisplayAttribute>(inherit: false);
|
||||||
|
if (display != null)
|
||||||
|
{
|
||||||
|
// Note [Display(Group = "")] is allowed.
|
||||||
|
var group = display.GetGroupName();
|
||||||
|
if (group != null)
|
||||||
|
{
|
||||||
|
return group;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1168,14 +1168,25 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures
|
||||||
}
|
}
|
||||||
|
|
||||||
var selectList = new List<SelectListItem>();
|
var selectList = new List<SelectListItem>();
|
||||||
foreach (var keyValuePair in metadata.EnumDisplayNamesAndValues)
|
var groupList = new Dictionary<string, SelectListGroup>();
|
||||||
|
foreach (var keyValuePair in metadata.EnumGroupedDisplayNamesAndValues)
|
||||||
{
|
{
|
||||||
var selectListItem = new SelectListItem
|
var selectListItem = new SelectListItem
|
||||||
{
|
{
|
||||||
Text = keyValuePair.Key,
|
Text = keyValuePair.Key.Name,
|
||||||
Value = keyValuePair.Value,
|
Value = keyValuePair.Value,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(keyValuePair.Key.Group))
|
||||||
|
{
|
||||||
|
if (!groupList.ContainsKey(keyValuePair.Key.Group))
|
||||||
|
{
|
||||||
|
groupList[keyValuePair.Key.Group] = new SelectListGroup() { Name = keyValuePair.Key.Group };
|
||||||
|
}
|
||||||
|
|
||||||
|
selectListItem.Group = groupList[keyValuePair.Key.Group];
|
||||||
|
}
|
||||||
|
|
||||||
selectList.Add(selectListItem);
|
selectList.Add(selectListItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -369,7 +369,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override IEnumerable<KeyValuePair<string, string>> EnumDisplayNamesAndValues
|
public override IEnumerable<KeyValuePair<EnumGroupAndName, string>> EnumGroupedDisplayNamesAndValues
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
||||||
Assert.Null(metadata.DisplayName);
|
Assert.Null(metadata.DisplayName);
|
||||||
Assert.Null(metadata.EditFormatString);
|
Assert.Null(metadata.EditFormatString);
|
||||||
Assert.Null(metadata.ElementMetadata);
|
Assert.Null(metadata.ElementMetadata);
|
||||||
Assert.Null(metadata.EnumDisplayNamesAndValues);
|
Assert.Null(metadata.EnumGroupedDisplayNamesAndValues);
|
||||||
Assert.Null(metadata.EnumNamesAndValues);
|
Assert.Null(metadata.EnumNamesAndValues);
|
||||||
Assert.Null(metadata.NullDisplayText);
|
Assert.Null(metadata.NullDisplayText);
|
||||||
Assert.Null(metadata.TemplateHint);
|
Assert.Null(metadata.TemplateHint);
|
||||||
|
|
|
||||||
|
|
@ -410,112 +410,112 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
// Type -> expected EnumDisplayNamesAndValues
|
// Type -> expected EnumDisplayNamesAndValues
|
||||||
public static TheoryData<Type, IEnumerable<KeyValuePair<string, string>>> EnumDisplayNamesData
|
public static TheoryData<Type, IEnumerable<KeyValuePair<EnumGroupAndName, string>>> EnumDisplayNamesData
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return new TheoryData<Type, IEnumerable<KeyValuePair<string, string>>>
|
return new TheoryData<Type, IEnumerable<KeyValuePair<EnumGroupAndName, string>>>
|
||||||
{
|
{
|
||||||
{ typeof(ClassWithFields), null },
|
{ typeof(ClassWithFields), null },
|
||||||
{ typeof(StructWithFields), null },
|
{ typeof(StructWithFields), null },
|
||||||
{ typeof(EmptyEnum), new List<KeyValuePair<string, string>>() },
|
{ typeof(EmptyEnum), new List<KeyValuePair<EnumGroupAndName, string>>() },
|
||||||
{ typeof(EmptyEnum?), new List<KeyValuePair<string, string>>() },
|
{ typeof(EmptyEnum?), new List<KeyValuePair<EnumGroupAndName, string>>() },
|
||||||
{
|
{
|
||||||
typeof(EnumWithDisplayNames),
|
typeof(EnumWithDisplayNames),
|
||||||
new List<KeyValuePair<string, string>>
|
new List<KeyValuePair<EnumGroupAndName, string>>
|
||||||
{
|
{
|
||||||
new KeyValuePair<string, string>(string.Empty, "0"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName("Zero", string.Empty), "0"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithDisplayNames.One), "1"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithDisplayNames.One)), "1"),
|
||||||
new KeyValuePair<string, string>("dos", "2"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, "dos"), "2"),
|
||||||
new KeyValuePair<string, string>("tres", "3"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, "tres"), "3"),
|
||||||
new KeyValuePair<string, string>("name from resources", "-2"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, "name from resources"), "-2"),
|
||||||
new KeyValuePair<string, string>("menos uno", "-1"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName("Negatives", "menos uno"), "-1"),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
typeof(EnumWithDisplayNames?),
|
typeof(EnumWithDisplayNames?),
|
||||||
new List<KeyValuePair<string, string>>
|
new List<KeyValuePair<EnumGroupAndName, string>>
|
||||||
{
|
{
|
||||||
new KeyValuePair<string, string>(string.Empty, "0"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName("Zero", string.Empty), "0"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithDisplayNames.One), "1"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithDisplayNames.One)), "1"),
|
||||||
new KeyValuePair<string, string>("dos", "2"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, "dos"), "2"),
|
||||||
new KeyValuePair<string, string>("tres", "3"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, "tres"), "3"),
|
||||||
new KeyValuePair<string, string>("name from resources", "-2"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, "name from resources"), "-2"),
|
||||||
new KeyValuePair<string, string>("menos uno", "-1"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName("Negatives", "menos uno"), "-1"),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// Note order duplicates appear cannot be inferred easily e.g. does not match the source.
|
// Note order duplicates appear cannot be inferred easily e.g. does not match the source.
|
||||||
// Zero is before None but Two is before Duece in the class below.
|
// Zero is before None but Two is before Duece in the class below.
|
||||||
typeof(EnumWithDuplicates),
|
typeof(EnumWithDuplicates),
|
||||||
new List<KeyValuePair<string, string>>
|
new List<KeyValuePair<EnumGroupAndName, string>>
|
||||||
{
|
{
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithDuplicates.Zero), "0"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithDuplicates.Zero)), "0"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithDuplicates.None), "0"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithDuplicates.None)), "0"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithDuplicates.One), "1"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithDuplicates.One)), "1"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithDuplicates.Duece), "2"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithDuplicates.Duece)), "2"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithDuplicates.Two), "2"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithDuplicates.Two)), "2"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithDuplicates.MoreThanTwo), "3"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithDuplicates.MoreThanTwo)), "3"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithDuplicates.Three), "3"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithDuplicates.Three)), "3"),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
typeof(EnumWithDuplicates?),
|
typeof(EnumWithDuplicates?),
|
||||||
new List<KeyValuePair<string, string>>
|
new List<KeyValuePair<EnumGroupAndName, string>>
|
||||||
{
|
{
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithDuplicates.Zero), "0"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithDuplicates.Zero)), "0"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithDuplicates.None), "0"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithDuplicates.None)), "0"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithDuplicates.One), "1"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithDuplicates.One)), "1"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithDuplicates.Duece), "2"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithDuplicates.Duece)), "2"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithDuplicates.Two), "2"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithDuplicates.Two)), "2"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithDuplicates.MoreThanTwo), "3"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithDuplicates.MoreThanTwo)), "3"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithDuplicates.Three), "3"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithDuplicates.Three)), "3"),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
typeof(EnumWithFlags),
|
typeof(EnumWithFlags),
|
||||||
new List<KeyValuePair<string, string>>
|
new List<KeyValuePair<EnumGroupAndName, string>>
|
||||||
{
|
{
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFlags.Zero), "0"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFlags.Zero)), "0"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFlags.One), "1"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFlags.One)), "1"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFlags.Two), "2"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFlags.Two)), "2"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFlags.Four), "4"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFlags.Four)), "4"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFlags.All), "-1"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFlags.All)), "-1"),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
typeof(EnumWithFlags?),
|
typeof(EnumWithFlags?),
|
||||||
new List<KeyValuePair<string, string>>
|
new List<KeyValuePair<EnumGroupAndName, string>>
|
||||||
{
|
{
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFlags.Zero), "0"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFlags.Zero)), "0"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFlags.One), "1"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFlags.One)), "1"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFlags.Two), "2"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFlags.Two)), "2"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFlags.Four), "4"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFlags.Four)), "4"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFlags.All), "-1"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFlags.All)), "-1"),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
typeof(EnumWithFields),
|
typeof(EnumWithFields),
|
||||||
new List<KeyValuePair<string, string>>
|
new List<KeyValuePair<EnumGroupAndName, string>>
|
||||||
{
|
{
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFields.Zero), "0"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFields.Zero)), "0"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFields.One), "1"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFields.One)), "1"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFields.Two), "2"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFields.Two)), "2"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFields.Three), "3"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFields.Three)), "3"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFields.MinusTwo), "-2"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFields.MinusTwo)), "-2"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFields.MinusOne), "-1"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFields.MinusOne)), "-1"),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
typeof(EnumWithFields?),
|
typeof(EnumWithFields?),
|
||||||
new List<KeyValuePair<string, string>>
|
new List<KeyValuePair<EnumGroupAndName, string>>
|
||||||
{
|
{
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFields.Zero), "0"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFields.Zero)), "0"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFields.One), "1"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFields.One)), "1"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFields.Two), "2"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFields.Two)), "2"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFields.Three), "3"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFields.Three)), "3"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFields.MinusTwo), "-2"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFields.MinusTwo)), "-2"),
|
||||||
new KeyValuePair<string, string>(nameof(EnumWithFields.MinusOne), "-1"),
|
new KeyValuePair<EnumGroupAndName, string>(new EnumGroupAndName(string.Empty, nameof(EnumWithFields.MinusOne)), "-1"),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
@ -524,9 +524,9 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[MemberData(nameof(EnumDisplayNamesData))]
|
[MemberData(nameof(EnumDisplayNamesData))]
|
||||||
public void GetDisplayMetadata_EnumDisplayNamesAndValues_ReflectsModelType(
|
public void GetDisplayMetadata_EnumGroupedDisplayNamesAndValues_ReflectsModelType(
|
||||||
Type type,
|
Type type,
|
||||||
IEnumerable<KeyValuePair<string, string>> expectedKeyValuePairs)
|
IEnumerable<KeyValuePair<EnumGroupAndName, string>> expectedKeyValuePairs)
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var provider = new DataAnnotationsMetadataProvider();
|
var provider = new DataAnnotationsMetadataProvider();
|
||||||
|
|
@ -541,8 +541,10 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
||||||
// Assert
|
// Assert
|
||||||
// OrderBy is used because the order of the results may very depending on the platform / client.
|
// OrderBy is used because the order of the results may very depending on the platform / client.
|
||||||
Assert.Equal(
|
Assert.Equal(
|
||||||
expectedKeyValuePairs?.OrderBy(item => item.Key, StringComparer.Ordinal),
|
expectedKeyValuePairs?.OrderBy(item => item.Key.Group, StringComparer.Ordinal)
|
||||||
context.DisplayMetadata.EnumDisplayNamesAndValues?.OrderBy(item => item.Key, StringComparer.Ordinal));
|
.ThenBy(item => item.Key.Name, StringComparer.Ordinal),
|
||||||
|
context.DisplayMetadata.EnumGroupedDisplayNamesAndValues?.OrderBy(item => item.Key.Group, StringComparer.Ordinal)
|
||||||
|
.ThenBy(item => item.Key.Name, StringComparer.Ordinal));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
@ -708,10 +710,10 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
||||||
[Display(ShortName = "uno")]
|
[Display(ShortName = "uno")]
|
||||||
One = 1,
|
One = 1,
|
||||||
|
|
||||||
[Display(Name = "")]
|
[Display(Name = "", GroupName = "Zero")]
|
||||||
Zero = 0,
|
Zero = 0,
|
||||||
|
|
||||||
[Display(Name = "menos uno")]
|
[Display(Name = "menos uno", GroupName = "Negatives")]
|
||||||
MinusOne = -1,
|
MinusOne = -1,
|
||||||
|
|
||||||
#if USE_REAL_RESOURCES
|
#if USE_REAL_RESOURCES
|
||||||
|
|
|
||||||
|
|
@ -112,8 +112,8 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
"\"http://schemas.datacontract.org/2004/07/MvcSample.Web.Models\"><About>I like playing Football" +
|
"\"http://schemas.datacontract.org/2004/07/MvcSample.Web.Models\"><About>I like playing Football" +
|
||||||
"</About><Address>My address</Address><Age>13</Age><Alive>true</Alive><Dependent><About i:nil=\"true\" />" +
|
"</About><Address>My address</Address><Age>13</Age><Alive>true</Alive><Dependent><About i:nil=\"true\" />" +
|
||||||
"<Address>Dependents address</Address><Age>0</Age><Alive>false</Alive><Dependent i:nil=\"true\" />" +
|
"<Address>Dependents address</Address><Age>0</Age><Alive>false</Alive><Dependent i:nil=\"true\" />" +
|
||||||
"<GPA>0</GPA><Log i:nil=\"true\" /><Name>Dependents name</Name>" +
|
"<EnumInformation>Zero</EnumInformation><GPA>0</GPA><Log i:nil=\"true\" /><Name>Dependents name</Name>" +
|
||||||
"<Profession i:nil=\"true\" /></Dependent><GPA>13.37</GPA><Log i:nil=\"true\" />" +
|
"<Profession i:nil=\"true\" /></Dependent><EnumInformation>Zero</EnumInformation><GPA>13.37</GPA><Log i:nil=\"true\" />" +
|
||||||
"<Name>My name</Name><Profession>Software Engineer</Profession></User>",
|
"<Name>My name</Name><Profession>Software Engineer</Profession></User>",
|
||||||
await response.Content.ReadAsStringAsync());
|
await response.Content.ReadAsStringAsync());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue