[英]How to create a List<selectListItem> in a controller using enum
Controller Controller
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;
}
Index指数
<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>
Enum枚举
public enum EnumFruits
{
[Description("Apple")]
A = 1,
[Description("Orange")]
B = 2,
[Description("Banana")]
C = 3,
[Description("Others")]
O = 0,
}
How can I use Enum instead of hardcoding it, Some of the post at here is for the dropdownlist but I wanted to append checkboxes instead of dropdownlist.如何使用枚举而不是硬编码它,这里的一些帖子是针对下拉列表的,但我想使用 append 复选框而不是下拉列表。 Is it possible to do it.
有没有可能做到。
Enum.GetValues
like below to achieve it.Enum.GetValues
来实现它。GetEnumDescription
helps you get description
of an enum.GetEnumDescription
的方法可帮助您获取枚举的description
。 Read the useful post here: 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();
You need extension method to get description from enum.您需要扩展方法才能从枚举中获取描述。
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.