I have an enum like this:
我有这样的枚举:
public enum PaymentType
{
Self=1,
Insurer=2,
PrivateCompany=3
}
And I am showing it as select box options like this inside Controller:
我在控制器中将它显示为像这样的选择框选项:
List<Patient.PaymentType> paymentTypeList =
Enum.GetValues(typeof (Patient.PaymentType)).Cast<Patient.PaymentType>().ToList();
ViewBag.PaymentType = new SelectList(paymentTypeList);
Here I can see that only the string part (example "Self") of the enum is going to the front end, so I won't get the value (example "1") of enum in my dropdown. How can I pass text as well as value of enum to select list?
在这里我可以看到只有枚举的字符串部分(例如“Self”)才会到达前端,所以我不会在我的下拉列表中获得枚举的值(例如“1”)。如何将文本以及枚举值传递给选择列表?
3 个解决方案
#1
4
You can write an extension method like this:
您可以编写这样的扩展方法:
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");
}
and in action use it like this:
并在行动中使用它像这样:
public ActionResult Test()
{
ViewBag.EnumList = PaymentType.Self.ToSelectList();
return View();
}
and in View :
在视图中:
@Html.DropDownListFor(m=>m.SomeProperty,ViewBag.EnumList as SelectList)
Rendered HTML:
<select id="EnumDropDown" name="EnumDropDown">
<option value="1">Self</option>
<option value="2">Insurer</option>
<option value="3">PrivateCompany</option>
</select>
Here is a working Demo Fiddle of Enum binding with DropDownListFor
这是一个使用DropDownListFor绑定Enum的Demo Fiddle
#2
1
public enum PaymentType
{
Self=1,
Insurer=2,
PrivateCompany=3
}
Get Self value:
获得自我价值:
int enumNumber = (int)PaymentType.Self; //enumNumber = 1
Exemple:
getEnum(PaymentType.Self);
private void getEnum(PaymentType t)
{
string enumName = t.ToString();
int enumNumber = (int)t;
MessageBox.Show(enumName + ": " + enumNumber.ToString());
}
#3
0
There is an extension method in MVC5 called SelectExtensions.EnumDropDownListFor which will generate the drop down list for you and bind the response back to an enum property in your model.
MVC5中有一个名为SelectExtensions.EnumDropDownListFor的扩展方法,它将为您生成下拉列表并将响应绑定回模型中的枚举属性。
#1
4
You can write an extension method like this:
您可以编写这样的扩展方法:
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");
}
and in action use it like this:
并在行动中使用它像这样:
public ActionResult Test()
{
ViewBag.EnumList = PaymentType.Self.ToSelectList();
return View();
}
and in View :
在视图中:
@Html.DropDownListFor(m=>m.SomeProperty,ViewBag.EnumList as SelectList)
Rendered HTML:
<select id="EnumDropDown" name="EnumDropDown">
<option value="1">Self</option>
<option value="2">Insurer</option>
<option value="3">PrivateCompany</option>
</select>
Here is a working Demo Fiddle of Enum binding with DropDownListFor
这是一个使用DropDownListFor绑定Enum的Demo Fiddle
#2
1
public enum PaymentType
{
Self=1,
Insurer=2,
PrivateCompany=3
}
Get Self value:
获得自我价值:
int enumNumber = (int)PaymentType.Self; //enumNumber = 1
Exemple:
getEnum(PaymentType.Self);
private void getEnum(PaymentType t)
{
string enumName = t.ToString();
int enumNumber = (int)t;
MessageBox.Show(enumName + ": " + enumNumber.ToString());
}
#3
0
There is an extension method in MVC5 called SelectExtensions.EnumDropDownListFor which will generate the drop down list for you and bind the response back to an enum property in your model.
MVC5中有一个名为SelectExtensions.EnumDropDownListFor的扩展方法,它将为您生成下拉列表并将响应绑定回模型中的枚举属性。