我使用的是asp.net mvc。這可能是一個直截了當的。我結合我的下拉列表,枚舉如下Dropdownlist綁定到一個枚舉
@Html.DropDownListFor(m => m.IndicatorGroups, Model.IndicatorGroups.ToSelectList(), new { @id = "ddlIndicatorGroup" })
型號定義如下
public class SearchControlViewModel
{
...
public GlobalEnums.IndicatorGroup IndicatorGroups { get; set; }
...
}
ToSelectList函數的定義如下
public static SelectList ToSelectList<TEnum>(this TEnum enumObj) where TEnum : struct, IComparable, IFormattable, IConvertible
{
var values = from TEnum e in Enum.GetValues(typeof(TEnum))
select new { Id = Convert.ToInt32(e), Name = e.ToString() };
return new SelectList(values, "Id", "Name", enumObj);
}
現在我已經添加了值的空格來枚舉,我想顯示這些值而不是「值與聯合國derscores「
public enum IndicatorGroup
{
[EnumMember(Value = "Include ANY MatchingIndicator")]
Include_ANY_MatchingIndicator = 1,
[EnumMember(Value = "Include ALL MatchingIndicator")]
Include_ALL_MatchingIndicator,
[EnumMember(Value = "Exclude ANY MatchingIndicator")]
Exclude_ANY_MatchingIndicator,
[EnumMember(Value = "Exclude ALL MatchingIndicator")]
Exclude_ALL_MatchingIndicator
};
我該如何做到這一點?
在'MVC-5',用['EnumDropDownListFor'](https://msdn.microsoft.com/en-us/library/system.web.mvc.html.selectextensions.enumdropdownlistfor%28v= vs.118%29.aspx)直接綁定enum到dropdownlist。 – mmushtaq
請參閱[這個答案](https://stackoverflow.com/questions/388483/how-do-you-create-a-dropdownlist-from-an-enum-in-asp-net-mvc)(使用'DescriptionAttribute ') –