c#List泛型数据扩展,把List<>型数据格式化成List<SelectListItem>,用来作dropdownlist的数据

代码例如以下

        public static List<SelectListItem> CreateSelect<T>(this IList<T> t, string text, string value)
        {
            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()} );
            }
            return l;
        }


 

原文地址:https://www.cnblogs.com/zhchoutai/p/7193675.html