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;
        }


 

posted @ 2017-07-17 12:21  zhchoutai  阅读(500)  评论(0)    收藏  举报