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" })




  • 相关阅读:
    怎么在本地建立一个Maven 项目push到码云(https://git.oschina.net)
    SmartGit(试用期30后),个人继续使用的方法。
    小项目的总结
    Http 协议简略
    servlet 的基础学习
    安卓访问https错误,访问http可以,可能是nginx ssl证书配置有问题
    EF Code first主从表,删除更新从表
    MSSQL 生成唯一自增数据的办法
    select2插件placeholder不显示的问题
    FormData的Ajax提交注意事项
  • 原文地址:https://www.cnblogs.com/zjmzone/p/2232719.html
Copyright © 2011-2022 走看看