Html.EnumDropdownListFor我可以按字母顺序排序吗?

时间:2023-01-26 14:05:21

I love the new Html.EnumDropdownListFor in MVC 5.1, and I see that I can specify the order of values within the Display attribute like this:

我喜欢MVC 5.1中新的Html.EnumDropdownListFor,我看到我可以在Display属性中指定值的顺序,如下所示:

    public enum AssignableDataFieldEnum
    {
        [Display(Name = "Code Value", Order=1)]
        CodeValue = 1,
        [Display(Name = "Final Digit", Order=2)]
        FinalDigit = 2,
        [Display(Name = "Group Number", Order=3)]
        GroupNumber = 3,
        [Display(Name = "Sequence Number", Order=4)]
        SequenceNumber = 4
}

This solution seems short sighted with localization. Is there a way to automatically have MVC order the DDL alphabetically for me?

这种解决方案似乎与本地化短视。有没有办法自动让MVC按字母顺序为我订购DDL?

2 个解决方案

#1


6  

I came up with a solution that gets the Enum values, sorts them, and then makes a call to HtmlHelper.DropDownListFor().

我想出了一个获取Enum值的解决方案,对它们进行排序,然后调用HtmlHelper.DropDownListFor()。

public static MvcHtmlString EnumSortedDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, string optionLabel = null, IDictionary<string, object> htmlAttributes = null) {
    ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
    var selectList = EnumHelper.GetSelectList(metadata.ModelType).OrderBy(i => i.Text).ToList();
    if (!String.IsNullOrEmpty(optionLabel) && selectList.Count != 0 && String.IsNullOrEmpty(selectList[0].Text)) {
        selectList[0].Text = optionLabel;
        optionLabel = null;
    }

    return htmlHelper.DropDownListFor(expression, selectList, optionLabel, htmlAttributes);
}

#2


1  

Is there a way to automatically have MVC order the DDL alphabetically for me?

有没有办法自动让MVC按字母顺序为我订购DDL?

I don't see how. None of the overloads appear to take any form of sorting parameters such as ASC or DESC. It seems like you'd either have to implement your own version of EnumDropDownListFor, potentially using EnumDropDownListFor itself, or use a javascript solution to sort the select element after the fact.

我不知道怎么样。没有任何重载似乎采用任何形式的排序参数,如ASC或DESC。看起来您要么必须实现自己的EnumDropDownListFor版本,可能使用EnumDropDownListFor本身,要么使用javascript解决方案对事后的select元素进行排序。

#1


6  

I came up with a solution that gets the Enum values, sorts them, and then makes a call to HtmlHelper.DropDownListFor().

我想出了一个获取Enum值的解决方案,对它们进行排序,然后调用HtmlHelper.DropDownListFor()。

public static MvcHtmlString EnumSortedDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, string optionLabel = null, IDictionary<string, object> htmlAttributes = null) {
    ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
    var selectList = EnumHelper.GetSelectList(metadata.ModelType).OrderBy(i => i.Text).ToList();
    if (!String.IsNullOrEmpty(optionLabel) && selectList.Count != 0 && String.IsNullOrEmpty(selectList[0].Text)) {
        selectList[0].Text = optionLabel;
        optionLabel = null;
    }

    return htmlHelper.DropDownListFor(expression, selectList, optionLabel, htmlAttributes);
}

#2


1  

Is there a way to automatically have MVC order the DDL alphabetically for me?

有没有办法自动让MVC按字母顺序为我订购DDL?

I don't see how. None of the overloads appear to take any form of sorting parameters such as ASC or DESC. It seems like you'd either have to implement your own version of EnumDropDownListFor, potentially using EnumDropDownListFor itself, or use a javascript solution to sort the select element after the fact.

我不知道怎么样。没有任何重载似乎采用任何形式的排序参数,如ASC或DESC。看起来您要么必须实现自己的EnumDropDownListFor版本,可能使用EnumDropDownListFor本身,要么使用javascript解决方案对事后的select元素进行排序。