zoukankan      html  css  js  c++  java
  • List转MVC DropDownListFor(SelectList)

     /// <summary>
            /// List转SelectListItem
            /// </summary>
            /// <typeparam name="T">Model对象</typeparam>
            /// <param name="t">集合</param>
            /// <param name="text">显示值-属性名</param>
            /// <param name="value">显示值-属性名</param>
            /// <param name="empId"></param>
            /// <returns></returns>
            public static List<SelectListItem> CreateSelect<T>(IList<T> t, string text, string value,string empId)
            {
                List<SelectListItem> l = new List<SelectListItem>();
                foreach (var item in t)
                {
                    var propers = item.GetType().GetProperty(text);
                    var valpropers = item.GetType().GetProperty(value);
                    l.Add(new SelectListItem
                    {
                        Text = propers.GetValue(item, null).ToString(), Value = valpropers.GetValue(item, null).ToString(),
                        Selected = valpropers.GetValue(item, null).ToString() == empId
                    });
                }
                return l;
            }

    调用:

    List<HrEmp> list = LoadData();
    List<SelectListItem> emplist = CreateSelect<HrEmp>(list, "EmpName", "EmpId",entity.HrEmpGuid.ToString());
    ViewData["Emp"] = new SelectList(emplist, "Value", "Text");

    视图调用:

      @Html.DropDownListFor(t => t.HrEmpGuid, ViewData["Emp"] as SelectList, new { @class = "form-control select2" })
    

      

  • 相关阅读:
    Ubuntu将Python3软连接到Python
    装有Ubuntu的硬盘插入到电脑中无法进入
    如何更改鼠标右键新建内容
    HDU 1113 Word Amalgamation
    暴力题,速算24点
    网络营销整合
    灰色预测代码
    灾情巡视C语言代码
    灰色关联度Matlab代码
    BP神经网络代码
  • 原文地址:https://www.cnblogs.com/zengdingding/p/5730086.html
Copyright © 2011-2022 走看看