我想获得一个Enum的值。在这里我得到的值为enumVal.ToCharArray()[0]。但它不正确。(当我在下拉列表中选择Received时,值显示我88(R的字符)
数组值,但我希望Received的值为1)
我的代码
var LetterTypeList = new List<LetterStatusModel>();
foreach (var item in Enum.GetValues(typeof(LetterStatus)))
{
string enumVal = item.ToString();
LetterTypeList.Add(new LetterStatusModel { LetterStatusCode = enumVal.ToCharArray()[0], LetterStatusName = enumVal });
}
public class LetterStatusModel
{
public int LetterStatusCode { get; set; }
public string LetterStatusName { get; set; }
}
public enum LetterStatus
{
Received = 1,
Issued = 2,
Paused = 3,
Cancel = 4,
}
答案 0 :(得分:3)
不是通过执行ToString()
获取值的名称,而是可以将值转换为int:
int enumValue = (int)item;