• 枚举的变换


    枚举用于选择下拉框

     1 /// <summary>
     2         /// 将枚举绑定到下拉框
     3         /// </summary>
     4         /// <typeparam name="T"></typeparam>
     5         /// <param name="p_Value">选中值</param>
     6         /// <param name="p_Tip">提示语</param>
     7         /// <param name="IsShowSelectTip">是否显示提示语</param>
     8         /// <param name="RemoveIndex">排除不显示的枚举值(索引)</param>
     9         /// <returns></returns>
    10         public static SelectList EnumToListItem<T>(int? p_Value, string p_Tip = "", bool IsShowSelectTip = true, int RemoveIndex = -10)
    11         {
    12             object _selectValue = null;
    13             if (p_Value.HasValue)
    14             {
    15                 _selectValue = p_Value.Value;
    16             }
    17             var item = new List<SelectListItem>();
    18             if (IsShowSelectTip)
    19             {
    20                 item.Add(new SelectListItem { Value = "", Text = "--请选择" + p_Tip + "--" });
    21             }
    22             foreach (int s in Enum.GetValues(typeof(T)))
    23             {
    24                 //item.Add(new SelectListItem() { Text = Enum.GetName(typeof(T), s), Value = s.ToString() });
    25                 if (RemoveIndex != s)
    26                 {
    27                     item.Add(new SelectListItem { Text = Enum.GetName(typeof(T), s), Value = s.ToString() });
    28                 }
    29             }
    30             var list = new SelectList(item, "Value", "Text", _selectValue);
    31             return list;
    32         }
    View Code

    将枚举转换为字典

     1 /// <summary>
     2         /// 将枚举转换成字典集合
     3         /// </summary>
     4         public static IDictionary<object, object> EnumToList<T>()
     5         {
     6             Dictionary<object, object> m_Dic = new Dictionary<object, object>();
     7             string m_Value = string.Empty;
     8             foreach (int s in Enum.GetValues(typeof(T)))
     9             {
    10                 m_Value = Enum.GetName(typeof(T), s);
    11                 m_Dic.Add(s, m_Value);
    12             }
    13             return m_Dic;
    14         }
    View Code
  • 相关阅读:
    WeChat小程序开发(五、前端总结)
    前端实现复制到剪贴板
    vue的自定义指令含大写字母会失效
    如何把网页变成黑白
    原生JS offsetX和offsetY引起抖动
    jQuery中prop方法和attr方法区别
    Js for循环中的闭包 & let和var的混用对比
    html和body标签默认高度为0带来的影响
    JS字符串数组降维
    CSS浮动流脱标的字围现象
  • 原文地址:https://www.cnblogs.com/HuberyHu/p/5319253.html
Copyright © 2020-2023  润新知