如何使用枚举在控制器中创建列表<selectListItem>
How to create a List<selectListItem> in a controller using enum
控制器
public ActionResult Index()
{
var view = new FruitVM();
ViewBag.SelectFruitList = GetFruitList();
return View(view);
}
public List<SelectListItem> GetFruitList()
{
List<SelectListItem> selectListItems = new List<SelectListItem>();
selectListItems.Add(new SelectListItem { Text = "Apple", Value = "1"});
selectListItems.Add(new SelectListItem { Text = "Orange", Value = "2" });
selectListItems.Add(new SelectListItem { Text = "Banana", Value = "3" });
selectListItems.Add(new SelectListItem { Text = "Others", Value = "0" });
return selectListItems;
}
索引
<div class="form-group">
<label class="col-sm-3">Pick Fruit</label>
<div class="col-sm-6">
@foreach (var item in (List<SelectListItem>)ViewBag.SelectFruitList)
{
<label class="checkbox-inline"><input type="checkbox" value="@item.Value" name="Fruit">@item.Text </label>
}
</div>
</div>
枚举
public enum EnumFruits
{
[Description("Apple")]
A = 1,
[Description("Orange")]
B = 2,
[Description("Banana")]
C = 3,
[Description("Others")]
O = 0,
}
如何使用 Enum 而不是对其进行硬编码,这里的一些 post 用于下拉列表,但我想附加复选框而不是下拉列表。可以做吗
- 像下面那样使用
Enum.GetValues
来实现它。
- 名为
GetEnumDescription
的方法可帮助您获得 description
枚举。在此处阅读有用的 post:How to get C# Enum description from value?
public static string GetEnumDescription(Enum value)
{
var fi = value.GetType().GetField(value.ToString());
var attributes = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
if (attributes != null && attributes.Any())
{
return attributes.First().Description;
}
return value.ToString();
}
var selectListItems = Enum.GetValues(typeof(EnumFruits))
.Cast<EnumFruits>()
.Select(v => new SelectListItem { Text = GetEnumDescription((EnumFruits)v), Value = (int)v } )
.ToList();
您需要扩展方法才能从枚举中获取描述。
var selectListItems = Enum.GetValues(typeof(EnumFruits))
.Cast<EnumFruits>()
.Select(v => new SelectListItem { Text = v.GetDescription(), Value = (int)v })
.ToList();
public static class EnumExtensions
{
private static TAttribute GetAttribute<TAttribute>(this Enum value)
where TAttribute : Attribute
{
var type = value?.GetType();
if (type == null)
return default;
var name = Enum.GetName(type, value);
return type.GetRuntimeField(name)?.GetCustomAttributes(false)
.OfType<TAttribute>().SingleOrDefault();
}
public static string GetDescription(this Enum value)
{
return value?.GetAttribute<DescriptionAttribute>()?.Description;
}
public static string GetDescription(this Enum value, params object[] args)
{
return string.Format(value?.GetAttribute<DisplayAttribute>()?.Name ?? string.Empty, args);
}
}
控制器
public ActionResult Index()
{
var view = new FruitVM();
ViewBag.SelectFruitList = GetFruitList();
return View(view);
}
public List<SelectListItem> GetFruitList()
{
List<SelectListItem> selectListItems = new List<SelectListItem>();
selectListItems.Add(new SelectListItem { Text = "Apple", Value = "1"});
selectListItems.Add(new SelectListItem { Text = "Orange", Value = "2" });
selectListItems.Add(new SelectListItem { Text = "Banana", Value = "3" });
selectListItems.Add(new SelectListItem { Text = "Others", Value = "0" });
return selectListItems;
}
索引
<div class="form-group">
<label class="col-sm-3">Pick Fruit</label>
<div class="col-sm-6">
@foreach (var item in (List<SelectListItem>)ViewBag.SelectFruitList)
{
<label class="checkbox-inline"><input type="checkbox" value="@item.Value" name="Fruit">@item.Text </label>
}
</div>
</div>
枚举
public enum EnumFruits
{
[Description("Apple")]
A = 1,
[Description("Orange")]
B = 2,
[Description("Banana")]
C = 3,
[Description("Others")]
O = 0,
}
如何使用 Enum 而不是对其进行硬编码,这里的一些 post 用于下拉列表,但我想附加复选框而不是下拉列表。可以做吗
- 像下面那样使用
Enum.GetValues
来实现它。 - 名为
GetEnumDescription
的方法可帮助您获得description
枚举。在此处阅读有用的 post:How to get C# Enum description from value?
public static string GetEnumDescription(Enum value)
{
var fi = value.GetType().GetField(value.ToString());
var attributes = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
if (attributes != null && attributes.Any())
{
return attributes.First().Description;
}
return value.ToString();
}
var selectListItems = Enum.GetValues(typeof(EnumFruits))
.Cast<EnumFruits>()
.Select(v => new SelectListItem { Text = GetEnumDescription((EnumFruits)v), Value = (int)v } )
.ToList();
您需要扩展方法才能从枚举中获取描述。
var selectListItems = Enum.GetValues(typeof(EnumFruits))
.Cast<EnumFruits>()
.Select(v => new SelectListItem { Text = v.GetDescription(), Value = (int)v })
.ToList();
public static class EnumExtensions
{
private static TAttribute GetAttribute<TAttribute>(this Enum value)
where TAttribute : Attribute
{
var type = value?.GetType();
if (type == null)
return default;
var name = Enum.GetName(type, value);
return type.GetRuntimeField(name)?.GetCustomAttributes(false)
.OfType<TAttribute>().SingleOrDefault();
}
public static string GetDescription(this Enum value)
{
return value?.GetAttribute<DescriptionAttribute>()?.Description;
}
public static string GetDescription(this Enum value, params object[] args)
{
return string.Format(value?.GetAttribute<DisplayAttribute>()?.Name ?? string.Empty, args);
}
}