当前位置: 代码网 > it编程>编程语言>Asp.net > ASP.NET MVC把数据库中枚举项的数字转换成文字

ASP.NET MVC把数据库中枚举项的数字转换成文字

2024年05月15日 Asp.net 我要评论
标题可能无法表达我的本意。比如,有这样一个枚举: public enum mychoice { myfirstchoice = 0, mysecondchoic

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

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

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

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com