我有一个这样的公开枚举:
public enum occupancyTimeline { TwelveMonths,FourteenMonths,SixteenMonths,EighteenMonths }
我将使用一个DropDown菜单,如下所示:
@Html.DropDownListFor(model => model.occupancyTimeline,new SelectList(Enum.GetValues(typeof(CentralParkLCPreview.Models.occupancyTimeline))),"")
现在我正在寻找我的价值观
12个月,14个月,16个月,18个月,而不是TweleveMonths,18个月
我该如何做到这一点?
解决方法
你可以检查这个
link
他的解决方案是针对Asp.NET,但是通过简单的修改,您可以在MVC中使用它
/// <span class="code-SummaryComment"><summary></span> /// Provides a description for an enumerated type. /// <span class="code-SummaryComment"></summary></span> [AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field,AllowMultiple = false)] public sealed class EnumDescriptionAttribute : Attribute { private string description; /// <span class="code-SummaryComment"><summary></span> /// Gets the description stored in this attribute. /// <span class="code-SummaryComment"></summary></span> /// <span class="code-SummaryComment"><value>The description stored in the attribute.</value></span> public string Description { get { return this.description; } } /// <span class="code-SummaryComment"><summary></span> /// Initializes a new instance of the /// <span class="code-SummaryComment"><see cref="EnumDescriptionAttribute"/> class.</span> /// <span class="code-SummaryComment"></summary></span> /// <span class="code-SummaryComment"><param name="description">The description to store in this attribute.</span> /// <span class="code-SummaryComment"></param></span> public EnumDescriptionAttribute(string description) : base() { this.description = description; } }
帮助者将允许您构建Key和Value列表
/// <span class="code-SummaryComment"><summary></span> /// Provides a static utility object of methods and properties to interact /// with enumerated types. /// <span class="code-SummaryComment"></summary></span> public static class EnumHelper { /// <span class="code-SummaryComment"><summary></span> /// Gets the <span class="code-SummaryComment"><see cref="DescriptionAttribute" /> of an <see cref="Enum" /></span> /// type value. /// <span class="code-SummaryComment"></summary></span> /// <span class="code-SummaryComment"><param name="value">The <see cref="Enum" /> type value.</param></span> /// <span class="code-SummaryComment"><returns>A string containing the text of the</span> /// <span class="code-SummaryComment"><see cref="DescriptionAttribute"/>.</returns></span> public static string GetDescription(Enum value) { if (value == null) { throw new ArgumentNullException("value"); } string description = value.ToString(); FieldInfo fieldInfo = value.GetType().GetField(description); EnumDescriptionAttribute[] attributes = (EnumDescriptionAttribute[]) fieldInfo.GetCustomAttributes(typeof(EnumDescriptionAttribute),false); if (attributes != null && attributes.Length > 0) { description = attributes[0].Description; } return description; } /// <span class="code-SummaryComment"><summary></span> /// Converts the <span class="code-SummaryComment"><see cref="Enum" /> type to an <see cref="IList" /> </span> /// compatible object. /// <span class="code-SummaryComment"></summary></span> /// <span class="code-SummaryComment"><param name="type">The <see cref="Enum"/> type.</param></span> /// <span class="code-SummaryComment"><returns>An <see cref="IList"/> containing the enumerated</span> /// type value and description.<span class="code-SummaryComment"></returns></span> public static IList ToList(Type type) { if (type == null) { throw new ArgumentNullException("type"); } ArrayList list = new ArrayList(); Array enumValues = Enum.GetValues(type); foreach (Enum value in enumValues) { list.Add(new KeyValuePair<Enum,string>(value,GetDescription(value))); } return list; } }
那么你将枚举你的枚举
public enum occupancyTimeline { [EnumDescriptionAttribute ("12 months")] TwelveMonths,[EnumDescriptionAttribute ("14 months")] FourteenMonths,[EnumDescriptionAttribute ("16 months")] SixteenMonths,[EnumDescriptionAttribute ("18 months")] EighteenMonths }
您可以在控制器中使用它来填充下拉列表
ViewBag.occupancyTimeline =new SelectList( EnumHelper.ToList(typeof(occupancyTimeline)),"Value","Key");
在您看来,您可以使用以下内容
@Html.DropdownList("occupancyTimeline")
希望会帮助你