如何将ASP.NET MVC中的枚举数字巧妙转换成数据库文字描述?

2026-03-30 11:241阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计657个文字,预计阅读时间需要3分钟。

如何将ASP.NET MVC中的枚举数字巧妙转换成数据库文字描述?

:无法准确表达我的意图。例如,有这样一种枚举:public enum MyChoice { MyFirstChoice=0, MySecondChoice=1, MyThirdChoice=2 }。数据库中,某表某字段存储的值为0、1、2,在显示时,我们希望表达的是第1个。

标题可能无法表达我的本意。比如,有这样一个枚举:

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。

如何将ASP.NET MVC中的枚举数字巧妙转换成数据库文字描述?

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(); }

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对易盾网络的支持。如果你想了解更多相关内容请查看下面相关链接

标签:数字

本文共计657个文字,预计阅读时间需要3分钟。

如何将ASP.NET MVC中的枚举数字巧妙转换成数据库文字描述?

:无法准确表达我的意图。例如,有这样一种枚举:public enum MyChoice { MyFirstChoice=0, MySecondChoice=1, MyThirdChoice=2 }。数据库中,某表某字段存储的值为0、1、2,在显示时,我们希望表达的是第1个。

标题可能无法表达我的本意。比如,有这样一个枚举:

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。

如何将ASP.NET MVC中的枚举数字巧妙转换成数据库文字描述?

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(); }

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对易盾网络的支持。如果你想了解更多相关内容请查看下面相关链接

标签:数字