如何解决如何获得Asp.Net Core DropdownList来显示C#枚举中的[Display]值?
我有一个看起来像这样的枚举:
BoT_DropdownValues.cs:
public class BoT_DropdownValues
{
public enum BlockSizes
{
[Display(Name = "12 Months")]
M12,[Display(Name = "24 Months")]
M24,[Display(Name = "36 Months")]
M36,[Display(Name = "48 Months")]
M48,[Display(Name = "60 Months")]
M60
}
我要从“选择”元素中选择一项:
CreateBoT.cshtml:
<div class="form-group" id="Q3_div">
@Html.DropDownListFor(m =>
m.BoT_Answers.Q3,new SelectList(Enum.GetValues(typeof(BoT_DropdownValues.BlockSizes))),"Select One",new { @class = "custom-select",@id = "BoT_Answers_Q3" })
...
问题:
此语法在下拉菜单中为我提供“ M12”,“ M24”,“ M36”,...。我会 Like 下拉菜单向用户显示[Display()]名称:“ 12个月”,“ 24个月”等。
问:@Html.DropDownListFor()
可以使用语法来实现这一点吗?
更新
我发现的大多数答案都涉及编写静态扩展类和许多额外的工作。我仍在寻找“更好的答案”,但是目前看来,这为我提供了我想要的一切:
@Html.DropDownListFor(m =>
m.BoT_Answers.Q3,new List<SelectListItem>
{
new SelectListItem {Text="Select One",Value="",Selected=true },new SelectListItem {Text="12 Months",Value="12" },new SelectListItem {Text="24 Months",Value="24" },new SelectListItem {Text="36 Months",Value="36" },new SelectListItem {Text="48 Months",Value="48" },new SelectListItem {Text="60 Months",Value="60" }
},@id = "BoT_Answers_Q3" })
问:有人能想到这种方法有什么缺点吗?
解决方法
https://m.youtube.com/watch?v=0JDxnjcH_v0
我最近观看的该视频似乎具有您想要的功能。如Dom所述,GetEnumSelectList可能就是您想要的。
,我使用辅助程序类进行了测试,并且有效。因此valuesToDisplay = [“ 12个月”,“ 24个月”等...]:
var valuesToDisplay = GetDisplayValues();
static List<string> GetDisplayValues() {
List<string> ret = new List<string>();
var enumValues = Enum.GetValues(typeof(BlockSizes));
foreach (var value in enumValues)
{
ret.Add(EnumHelper<BlockSizes>.GetDisplayValue((BlockSizes)value));
}
return ret;
}
public static class EnumHelper<T>
{
public static IList<T> GetValues(Enum value)
{
var enumValues = new List<T>();
foreach (FieldInfo fi in value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public))
{
enumValues.Add((T)Enum.Parse(value.GetType(),fi.Name,false));
}
return enumValues;
}
public static T Parse(string value)
{
return (T)Enum.Parse(typeof(T),value,true);
}
public static IList<string> GetNames(Enum value)
{
return value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public).Select(fi => fi.Name).ToList();
}
public static IList<string> GetDisplayValues(Enum value)
{
return GetNames(value).Select(obj => GetDisplayValue(Parse(obj))).ToList();
}
private static string lookupResource(Type resourceManagerProvider,string resourceKey)
{
foreach (PropertyInfo staticProperty in resourceManagerProvider.GetProperties(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public))
{
if (staticProperty.PropertyType == typeof(System.Resources.ResourceManager))
{
System.Resources.ResourceManager resourceManager = (System.Resources.ResourceManager)staticProperty.GetValue(null,null);
return resourceManager.GetString(resourceKey);
}
}
return resourceKey; // Fallback with the key name
}
public static string GetDisplayValue(T value)
{
var fieldInfo = value.GetType().GetField(value.ToString());
var descriptionAttributes = fieldInfo.GetCustomAttributes(
typeof(DisplayAttribute),false) as DisplayAttribute[];
if (descriptionAttributes[0].ResourceType != null)
return lookupResource(descriptionAttributes[0].ResourceType,descriptionAttributes[0].Name);
if (descriptionAttributes == null) return string.Empty;
return (descriptionAttributes.Length > 0) ? descriptionAttributes[0].Name : value.ToString();
}
}
,
您将需要编写一种方法或使用其他人的方法。这是我的;
public static class EnumHelper<T> where T:struct,Enum{
public static List<(T value,string display)> Values =
typeof(T)
.GetFields(BindingFlags.Static | BindingFlags.Public)
.OrderBy(f => f.MetadataToken)
.Select(f => (
(T)f.GetValue(null),f.GetCustomAttribute<DisplayNameAttribute>()?.DisplayName
?? f.GetCustomAttribute<DisplayAttribute>()?.Name
?? f.Name
))
.ToList();
}
public static class Extensions{
public static IEnumerable<SelectListItem> SelectList<T>(this IHtmlHelper html) where T:struct,Enum
=> EnumHelper<T>.Values.Select(v => new SelectListItem(v.display,v.value.ToString()));
}
<select asp-items="Html.SelectList<BlockSizes>()"></select>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。