예제 #1
0
        public static SelectList GenericSelectList(AssetManagerContext db, Type entityType
            , string property, object selectedvalue)
        {
            var set = db.Set(entityType);
            var query = set.OrderBy(property)
                .Select("new(" + property + ")")
                .Distinct()
                ;

            SelectList selectlist = new SelectList(query, property, property, selectedvalue);
            string selected = selectedvalue.ToStringOrEmpty();

            if (!string.IsNullOrWhiteSpace(selected) && !selectlist.Contains(selectedvalue))
            {
                List<SelectListItem> list = selectlist.ToList();
                list.Add(new SelectListItem { Text = selected, Value = selected });
                list.Sort((x, y) => x.Text.CompareTo(y.Text));
                selectlist = new SelectList(list, "Value", "Text", selectedvalue);
            }
            return selectlist;
        }