获取枚举值以显示在下拉列表 Asp.Net MVC
Get Enum value to show on Dropdownlist Asp.Net MVC
我有一个这样的枚举:
public enum PaymentType
{
Self=1,
Insurer=2,
PrivateCompany=3
}
我在控制器中将其显示为 select 框选项,如下所示:
List<Patient.PaymentType> paymentTypeList =
Enum.GetValues(typeof (Patient.PaymentType)).Cast<Patient.PaymentType>().ToList();
ViewBag.PaymentType = new SelectList(paymentTypeList);
在这里我可以看到只有枚举的字符串部分(例子"Self")是去前端的,所以我不会得到我的枚举的值(例子“1”)落下。如何将文本和枚举值传递给 select 列表?
MVC5 中有一个名为 SelectExtensions.EnumDropDownListFor 的扩展方法,它会为您生成下拉列表并将响应绑定回您模型中的枚举 属性。
public enum PaymentType
{
Self=1,
Insurer=2,
PrivateCompany=3
}
获取自我价值:
int enumNumber = (int)PaymentType.Self; //enumNumber = 1
示例:
getEnum(PaymentType.Self);
private void getEnum(PaymentType t)
{
string enumName = t.ToString();
int enumNumber = (int)t;
MessageBox.Show(enumName + ": " + enumNumber.ToString());
}
你可以这样写一个扩展方法:
public static System.Web.Mvc.SelectList ToSelectList<TEnum>(this TEnum obj)
where TEnum : struct, IComparable, IFormattable, IConvertible // correct one
{
return new SelectList(Enum.GetValues(typeof(TEnum)).OfType<Enum>()
.Select(x =>
new SelectListItem
{
Text = Enum.GetName(typeof(TEnum), x),
Value = (Convert.ToInt32(x)).ToString()
}), "Value", "Text");
}
并在实际中像这样使用它:
public ActionResult Test()
{
ViewBag.EnumList = PaymentType.Self.ToSelectList();
return View();
}
并在视图中:
@Html.DropDownListFor(m=>m.SomeProperty,ViewBag.EnumList as SelectList)
已渲染 HTML:
<select id="EnumDropDown" name="EnumDropDown">
<option value="1">Self</option>
<option value="2">Insurer</option>
<option value="3">PrivateCompany</option>
</select>
这是一个working Demo Fiddle of Enum binding with DropDownListFor
我有一个这样的枚举:
public enum PaymentType
{
Self=1,
Insurer=2,
PrivateCompany=3
}
我在控制器中将其显示为 select 框选项,如下所示:
List<Patient.PaymentType> paymentTypeList =
Enum.GetValues(typeof (Patient.PaymentType)).Cast<Patient.PaymentType>().ToList();
ViewBag.PaymentType = new SelectList(paymentTypeList);
在这里我可以看到只有枚举的字符串部分(例子"Self")是去前端的,所以我不会得到我的枚举的值(例子“1”)落下。如何将文本和枚举值传递给 select 列表?
MVC5 中有一个名为 SelectExtensions.EnumDropDownListFor 的扩展方法,它会为您生成下拉列表并将响应绑定回您模型中的枚举 属性。
public enum PaymentType
{
Self=1,
Insurer=2,
PrivateCompany=3
}
获取自我价值:
int enumNumber = (int)PaymentType.Self; //enumNumber = 1
示例:
getEnum(PaymentType.Self);
private void getEnum(PaymentType t)
{
string enumName = t.ToString();
int enumNumber = (int)t;
MessageBox.Show(enumName + ": " + enumNumber.ToString());
}
你可以这样写一个扩展方法:
public static System.Web.Mvc.SelectList ToSelectList<TEnum>(this TEnum obj)
where TEnum : struct, IComparable, IFormattable, IConvertible // correct one
{
return new SelectList(Enum.GetValues(typeof(TEnum)).OfType<Enum>()
.Select(x =>
new SelectListItem
{
Text = Enum.GetName(typeof(TEnum), x),
Value = (Convert.ToInt32(x)).ToString()
}), "Value", "Text");
}
并在实际中像这样使用它:
public ActionResult Test()
{
ViewBag.EnumList = PaymentType.Self.ToSelectList();
return View();
}
并在视图中:
@Html.DropDownListFor(m=>m.SomeProperty,ViewBag.EnumList as SelectList)
已渲染 HTML:
<select id="EnumDropDown" name="EnumDropDown">
<option value="1">Self</option>
<option value="2">Insurer</option>
<option value="3">PrivateCompany</option>
</select>
这是一个working Demo Fiddle of Enum binding with DropDownListFor