zoukankan      html  css  js  c++  java
  • Razor

    //
    //仅供学习,注明出处。
    //PlanUseOfDrugsVM页面层对象模型
    //这里是Controller控制器方法。通过制定方法获取枚举,运用ViewData存取键值对数据
     public IList<PlanUseOfDrugsVM> GetIntervalType()
    {
    IList<PlanUseOfDrugsVM> list = new List<PlanUseOfDrugsVM>();
    IDictionary<int, string> dic = new GetDicFromEnum().Get(typeof(IntervalEnum));
    list.Add(new PlanUseOfDrugsVM
    {
    Interval = null,
    IntervalName = "请选择..."
    });
    foreach (var p in dic)
    {
    list.Add(new PlanUseOfDrugsVM { Interval = p.Key, IntervalName = p.Value });

    }
    return list;
    }
    //仅供学习,注明出处。
    public class GetDicFromEnum
    {
    ///<summary>
    /// 根据一个枚举类型得到一个IDictionary格式如下
    /// public enum Folk
    /// {
    /// [Description("汉族")]
    /// HanZu,
    /// [Description("回族")]
    /// Huizi
    /// }
    ///</summary>
    ///<param name="enumType">枚举类型</param>
    ///<returns>IDictionary列表</returns>
    public IDictionary<int, string> Get(Type enumType)
    {
    IDictionary<int, string> ht = new Dictionary<int, string>();
    Type typeDescription = typeof(DescriptionAttribute);
    System.Reflection.FieldInfo[] fields = enumType.GetFields();
    string strText = string.Empty;
    int strValue = 0;
    foreach (FieldInfo field in fields)
    {
    if (field.FieldType.IsEnum == true)
    {
    strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null));
    if (strValue >= -1)
    {
    object[] arr = field.GetCustomAttributes(typeDescription, true);
    if (arr.Length > 0)
    {
    DescriptionAttribute aa = (DescriptionAttribute)arr[0];
    strText = aa.Description;
    }
    else
    {
    strText = field.Name;
    }
    ht.Add(strValue, strText);
    }
    }
    }

    return ht;
    }
    }
    }
    //仅供学习,注明出处。
    //前台DropDownList引用
    
    
    
    
     @Html.LabelFor(model => model.MType):
    @Html.DropDownListFor(model => model.MType,
    ViewData["GetIntervalType"] as SelectList, new { @style = "160px" })




  • 相关阅读:
    数据结构之fhq-treap
    [AtCoder Regular Contest 096 E] Everything on It 解题报告 (第二类斯特林数+容斥原理)
    指纹识别人脸识别 iOS
    HTTP协议的8种请求类型介绍
    获取已安装app的bundle id
    iOS生成Bundle包及使用
    为什么说Objective-C是一门动态的语言?
    引用外部静态库(.a文件)时或打包.a时,Category方法无法调用。崩溃
    代码混淆 iOS
    HDU 1695 GCD(莫比乌斯反演)
  • 原文地址:https://www.cnblogs.com/zjmzone/p/2232719.html
Copyright © 2011-2022 走看看