ASP.NET MVC把数据库中枚举项的数字转换成文字
作者:Darren 时间:2024-01-16 07:35:39
标题可能无法表达我的本意。比如,有这样一个枚举:
public enum MyChoice
{
MyFirstChoice = 0,
MySecondChoice =1,
MyThirdChoice = 2
}
数据库中,某表某字段保存值为"0,1,2",在显示的时候,我们希望是"第一个选择,第二个选择,第三个选择"。如何做呢?
可以为枚举项上面标注自定义特性。先自定义一个特性如下:
public class EnumDisplayNameAttribute : Attribute
{
private string _displayName;
public EnumDisplayNameAttribute(string displayName)
{
_displayName = displayName;
}
public string DisplayName
{
get
{
return _displayName;
}
}
}
然后,把自定义特性标注放到枚举项上去。
public enum MyChoice
{
[EnumDisplayName("我的第一个选择")]
MyFirstChoice = 0,
[EnumDisplayName("我的第二个选择")]
MySecondChoice =1,
[EnumDisplayName("我的第三个选择")]
MyThirdChoice = 2
}
现在,需要一个帮助方法,能读出枚举项上的自定义特性EnumDisplayName。
public class EnumExt
{
/// <summary>
/// 获取枚举项的注释
/// </summary>
/// <param name="e">枚举项</param>
/// <returns></returns>
public static string GetEnumDescription(object e)
{
//获取枚举项
Type t = e.GetType();
//获取枚举项的字段
FieldInfo[] fis = t.GetFields();
foreach (FieldInfo fi in fis)
{
//如果当前字段名称不是当前枚举项
if (fi.Name != e.ToString())
{
continue;//结束本次循环
}
//如果当前字段的包含自定义特性
if (fi.IsDefined(typeof (EnumDisplayNameAttribute), true))
{
//获取自定义特性的属性值
return (fi.GetCustomAttributes(typeof(EnumDisplayNameAttribute), true)[0] as EnumDisplayNameAttribute).DisplayName;
}
}
return e.ToString();
}
public static List<SelectListItem> GetSelectList(Type enumType)
{
List<SelectListItem> selectList = new List<SelectListItem>();
//selectList.Add(new SelectListItem{Text = "--请选择--",Value = ""});
foreach (object e in Enum.GetValues(enumType))
{
selectList.Add(new SelectListItem { Text = GetEnumDescription(e), Value = ((int)e).ToString() });
}
return selectList;
}
}
以上,
GetEnumDescription方法根据枚举项获取其上的自定义特性EnumDisplayNameAttribute的DisplayName属性值。
GetSelectList方法根据枚举的Type类型返回SelectListItem集合,通常在ASP.NET MVC中使用。
最后,就能实现本篇的需求:
static void Main(string[] args)
{
string myChoiceInt = "0,1,2";
string[] choiceArr = myChoiceInt.Split(',');
string temp = string.Empty;
foreach (string item in choiceArr)
{
//转换成枚举的类型
short enumValShort = short.Parse(item);
temp = temp + EnumExt.GetEnumDescription((MyChoice)enumValShort) + ",";
}
Console.WriteLine(temp.Substring(0, temp.Length - 1));
Console.ReadKey();
}
来源:https://www.cnblogs.com/darrenji/p/4457328.html
标签:ASP.NET,MVC,数据库,枚举项,数字,文字
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JScript下Array对象的性能问题
2009-02-15 12:28:00
PyCharm连接远程服务器的超级详细教程
2023-05-10 16:59:31
![](https://img.aspxhome.com/file/2023/6/135256_0s.jpg)
SQLServer用t-sql命令批量删除数据库中指定表(游标循环删除)
2024-01-16 15:45:54
python中pandas.DataFrame对行与列求和及添加新行与列示例
2023-03-16 22:44:01
Jupyter Notebook读取csv文件出现的问题及解决
2023-08-09 23:11:50
![](https://img.aspxhome.com/file/2023/9/64649_0s.jpg)
list视图方式设计浅析
2008-12-21 16:04:00
![](https://img.aspxhome.com/file/UploadPic/200812/21/01-28s.jpg)
Vue3中defineEmits、defineProps 不用引入便直接用
2024-05-09 09:53:01
python txt中的文件,逐行读取并且每行赋值给变量问题
2021-04-28 21:12:02
![](https://img.aspxhome.com/file/2023/1/117341_0s.png)
Golang栈结构和后缀表达式实现计算器示例
2024-05-02 16:25:09
![](https://img.aspxhome.com/file/2023/5/130715_0s.jpg)
关于人物角色设计讨论
2008-10-16 13:47:00
![](https://img.aspxhome.com/file/UploadPic/200810/16/20081016134946734s.jpg)
Vue项目中配置pug解析支持
2024-05-09 09:52:53
Linux环境MySQL服务器级优化讲解
2008-12-04 17:21:00
js实现页面图片消除效果
2024-04-28 10:20:54
python+rsync精确同步指定格式文件
2023-09-18 06:51:26
深入理解Django中内置的用户认证
2022-01-25 10:05:43
利用Python3实现统计大量单词中各字母出现的次数和频率的方法
2022-04-18 09:29:50
![](https://img.aspxhome.com/file/2023/5/77685_0s.png)
初衷和结果
2009-02-23 12:52:00
阿里云CentOS7搭建Apache+PHP+MySQL环境
2023-11-23 02:44:59
Python多进程的使用详情
2022-09-21 23:55:58
Python运算符+与+=的方法实例
2022-02-26 04:40:59