zoukankan      html  css  js  c++  java
  • html中select标签根据枚举获得值的总结

    不知不觉在公司一个多月了,这一个月做了一个支票申请的web页面功能,都不是特别难,审核有公司给的工作流,分页工具和很多公用工具公司也都给了,所以觉得难度都不是很大。今天主管让我们修改了以前做的项目的代码规范,有一个问题是html的select标签绑定数据问题,不能用之前写死的,而是要求根据枚举然后动态给select绑定数据,这是我在公司带的遇到的第二个大问题了。

    枚举的基础知识网上都有,就不说了,我想说一下枚举除了名称和值以外还一个特性,那就是描述,而一般值是int,名称是英文,描述可以是中文作为select的option显示,定义如下:

    public enum Url{
        [Description("http://www.thylx.net")]        
        个人博客 = 1,
        [Description("http://blog.163.com/thylx133@126/")]
        网易博客 = 2,        
        [Description("http://www.8eshare.com/")]
        八邑分享 = 3
    }

    然后就是获取C#枚举中的描述值的方法了:

      /// <summary>
            /// 获取描述信息
            /// </summary>
            /// <param name="en">枚举</param>
            /// <returns></returns>
            public static string GetEnumDes(Enum en) {
                    Type type = en.GetType();
                    MemberInfo[] memInfo = type.GetMember(en.ToString()); 
                    if (memInfo != null && memInfo.Length > 0)
                    {
                            object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
                            if (attrs != null && attrs.Length > 0)
                            return ((DescriptionAttribute)attrs[0]).Description;
                    }           
                    return en.ToString(); 
           }

    这些都是从网上po来的,谢谢那位大神,这个写得很清楚了,然后再附上一些enum转int,int转enum,string转enum的方法:

    Colors color = (Colors)2              int-》enum(个人觉得这个方法很好用)

    (int)Colors.Red                           enum-》int

    (Colors)Enum.Parse(typeof(Colors), "Red")                    string-》enum

    Enum.GetName(typeof(Colors),3))                                enum-》string

    还有一些enum常用的方法,GetValues和GetName(这个可以查看msdn帮助文档)。

  • 相关阅读:
    165. Compare Version Numbers
    164. Maximum Gap
    3、桶排序
    162. Find Peak Element
    160. Intersection of Two Linked Lists
    155. Min Stack
    154. Find Minimum in Rotated Sorted Array II
    153. Find Minimum in Rotated Sorted Array
    Linux/Unix系统编程手册 第二章:基本概念
    Linux/Unix系统编程手册 第一章:历史和标准
  • 原文地址:https://www.cnblogs.com/junshijie/p/5177612.html
Copyright © 2011-2022 走看看