我有我這樣的枚舉。Enum有多個描述
public enum Gender
{
Man = 1,
Woman = 2
}
而且我使用ASP MVC4在下拉菜單中顯示選項。
@Html.DropDownListFor(model => model.Gender, new SelectList(Enum.GetValues(typeof(Namespace.Models.Enum.Gender))))
這工作就像一個魅力,它顯示男人/女人在下拉。 我的問題是,我想在不同的上下文中顯示不同名稱的枚舉。
就像一個環境會是如果你是一個媽媽或爸爸。我想使用性別枚舉作爲基礎,但顯示男人/女人的媽媽/爸爸instad。
另一種情況是男孩/女孩,我仍然想使用性別枚舉,但顯示不同的文本。
這有可能以任何方式嗎?
編輯 我使用凱文的解決方案,並且還添加了另一個像這樣的擴展方法。
public static List<KeyValuePair<string, int>> GetValues(IGenderStrategy genderStrategy)
{
Dictionary<string, int> arr = new Dictionary<string, int>();
foreach (Gender g in System.Enum.GetValues(typeof(Gender)))
arr.Add(g.ToValue(genderStrategy), (int)g);
return arr.ToList();
}
在我看來,我喜歡這樣用。
@Html.DropDownListFor(model => model.Gender, new SelectList(Chores.Models.Enum.EnumExtentions.GetValues(new Chores.Models.Enum.ParentStrategy()), "value", "key"))
這可能讓你開始。 http://stackoverflow.com/questions/13499747/how-to-get-selectlist-to-honor-displayname-annotation-with-enums?rq=1 –
我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –
好的,謝謝。我會記住這一點。 – Martin