Exemplo n.º 1
0
        public static List <SelectListItem> ToSelectListItem <T>(IEnumerable <T> enumerable, Func <T, string> getValue, Func <T, string> getText,
                                                                 SelectListFirstElementType firstElement, string selectedValue)
        {
            List <SelectListItem> selectedList = new List <SelectListItem>();

            string firtsText = SelectListFirstElementName.EMPTY;

            switch (firstElement)
            {
            case SelectListFirstElementType.Empty:
                break;

            case SelectListFirstElementType.None:
                firtsText = SelectListFirstElementName.NONE;
                break;

            case SelectListFirstElementType.Select:
                firtsText = SelectListFirstElementName.SELECT;
                break;

            case SelectListFirstElementType.All:
                firtsText = SelectListFirstElementName.ALL;
                break;
            }

            selectedList.Add(new SelectListItem {
                Text = firtsText, Value = string.Empty, Selected = (selectedValue == "" ? true : false)
            });

            if (enumerable != null)
            {
                var tempList = enumerable
                               .Select(x => new SelectListItem
                {
                    Selected = (getValue(x) == selectedValue),
                    Text     = getText(x),
                    Value    = getValue(x)
                })
                               .ToList();

                selectedList.AddRange(tempList);
            }

            return(selectedList);
        }
Exemplo n.º 2
0
 public static List <SelectListItem> ToSelectListItem <T>(SelectListFirstElementType firstElement)
 {
     return(ToSelectListItem <T>(null, null, null, firstElement, String.Empty));
 }