【发布时间】:2015-03-02 10:26:23
【问题描述】:
我有一个这样的枚举:
public enum PaymentType
{
Self=1,
Insurer=2,
PrivateCompany=3
}
我在 Controller 中将它显示为这样的选择框选项:
List<Patient.PaymentType> paymentTypeList =
Enum.GetValues(typeof (Patient.PaymentType)).Cast<Patient.PaymentType>().ToList();
ViewBag.PaymentType = new SelectList(paymentTypeList);
在这里我可以看到只有枚举的字符串部分(例如“Self”)会进入前端,所以我不会在下拉列表中获得枚举的值(例如“1”)。如何将文本和枚举值传递给选择列表?
【问题讨论】:
-
您不一定需要,因为
DefaultModelBinder会在您回发时绑定到字符串值,但these answers 显示了一些方法来做到这一点。 -
只需将其转换为
int,它将为您提供int值。 -
您是否尝试过搜索? stackoverflow.com/questions/388483/… 等等。
标签: c# asp.net asp.net-mvc enums