Mvc: `GetEnumSelectList` does not appear to respect Order property of DisplayAttribute

Created on 15 Mar 2016  路  8Comments  路  Source: aspnet/Mvc

I would expect GetEnumSelectList<TEnum> to respect the Order property of any DisplayAttribute defined on the enum's members. For groups, I would expect the Order property to apply within the scope of a group. Using rc1 this does not appear to be the case.

Perhaps this could be done when building up an instance of DataAnnonationsMetadataProvider.

3 - Done bug

Most helpful comment

Yeah this is all fine and I'd be ok one day implementing this, but it's just not a priority for shipping right now.

All 8 comments

Might not be a bad idea though I'm not convinced [Display(Order = x)] is common on enum fields.

Note ModelMetadata.EnumGroupedDisplayNamesAndValues and ...EnumNamesAndValues already have a strict order. They match Enum.GetNames() and that, in turn, sorts enum names and values using their values. I'm therefore curious: What scenario leads to a poor <option> ordering when using the existing GetEnumSelectList<TEnum>() implementation?

In cases where an enum's members' underlying values are implicitly assigned (something I hate to be honest) one can re-order the members for purely presentational purposes without breaking things by first going through and explicitly declaring the underlying values. But even when an enum's underlying values are explicitly declared, it makes more sense from a developer's point of view to keep the members ordered by their underlying values, especially when new members may be added over time for business reasons. But from the user's point of view, members that are added over time may need to appear in a different order than they do in code.

So it's kind of a small thing, but if we're committed to the [Display] attribute why not go all the way? :wink:

Yeah this is all fine and I'd be ok one day implementing this, but it's just not a priority for shipping right now.

@Eilon @dougbu EnumGroupedDisplayNamesAndValues is an IEnumerable<KeyValuePair<EnumGroupAndName, string>> and underneath you've guaranteed the order by using a List<KeyValuePair<EnumGroupAndName, string>>. EnumNamesAndValues, however, is a IReadOnlyDictionary<string, string>, and dictionaries do not guarantee order.

I was planning to submit a PR for this just now but that is a breaking change... would it be considered for 2.0.0 to change that to an IEnumerable<KeyValuePair<string, string>>?

@tuespetre what is the use case for ordering both properties?

@dougbu good question. I guess EnumNamesAndValues is not used ~anywhere.~ for displaying the list of values.

@dougbu can you review the PR at https://github.com/aspnet/Mvc/pull/6012/files ?

575fe68

Was this page helpful?
0 / 5 - 0 ratings