Esempio n. 1
0
        public static MvcHtmlString LocalizedEnumDropDownListFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper
                                                                                     , Expression <Func <TModel, TProperty> > expression
                                                                                     , string optionLabel
                                                                                     , object htmlAttributes)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression <TModel, TProperty>(expression, htmlHelper.ViewData);

            if (metadata == null)
            {
                throw new ArgumentException("metad data is null", nameof(expression));
            }
            if (metadata.ModelType == (Type)null)
            {
                throw new ArgumentException("metadata type is null", nameof(expression));
            }
            if (!EnumHelper.IsValidForEnumHelper(metadata.ModelType))
            {
                throw new ArgumentException("value is not enum", nameof(expression));
            }

            string expressionText    = ExpressionHelper.GetExpressionText((LambdaExpression)expression);
            string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expressionText);
            Enum   @enum             = (Enum)null;

            if (!string.IsNullOrEmpty(fullHtmlFieldName))
            {
                @enum = GetModelStateValue(htmlHelper, fullHtmlFieldName, metadata.ModelType) as Enum;
            }
            if (@enum == null && !string.IsNullOrEmpty(expressionText))
            {
                @enum = htmlHelper.ViewData.Eval(expressionText) as Enum;
            }
            if (@enum == null)
            {
                @enum = metadata.Model as Enum;
            }
            IList <SelectListItem> selectList = EnumHelperExtension.GetSelectList(metadata.ModelType, @enum);

            if (!string.IsNullOrEmpty(optionLabel) && selectList.Count != 0 && string.IsNullOrEmpty(selectList[0].Text))
            {
                selectList[0].Text  = optionLabel;
                selectList[0].Value = "";
                optionLabel         = (string)null;
            }

            return(htmlHelper.BuildEnumDropDown(metadata, optionLabel, fullHtmlFieldName, selectList, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)));
        }
Esempio n. 2
0
        public static IList <SelectListItem> GetSelectList(Type type, Enum value)
        {
            IList <SelectListItem> selectList = EnumHelperExtension.GetSelectList(type);
            Type type1 = value == null ? (Type)null : value.GetType();

            if (type1 != (Type)null && type1 != type && type1 != Nullable.GetUnderlyingType(type))
            {
                throw new ArgumentException("value");
            }

            if (value == null && selectList.Count != 0 && string.IsNullOrEmpty(selectList[0].Value))
            {
                selectList[0].Selected = true;
            }
            else
            {
                string str  = value == null ? "0" : value.ToString("d");
                bool   flag = false;
                for (int index = selectList.Count - 1; !flag && index >= 0; --index)
                {
                    SelectListItem selectListItem = selectList[index];
                    selectListItem.Selected = str == selectListItem.Value;
                    flag |= selectListItem.Selected;
                }
                if (!flag)
                {
                    if (selectList.Count != 0 && string.IsNullOrEmpty(selectList[0].Value))
                    {
                        selectList[0].Selected = true;
                        selectList[0].Value    = str;
                    }
                    else
                    {
                        selectList.Insert(0, new SelectListItem()
                        {
                            Selected = true,
                            Text     = string.Empty,
                            Value    = str
                        });
                    }
                }
            }
            return(selectList);
        }
Esempio n. 3
0
        public static IList <SelectListItem> GetSelectList(Type type)
        {
            if (type == (Type)null)
            {
                throw new ArgumentNullException("type");
            }
            if (!EnumHelper.IsValidForEnumHelper(type))
            {
                throw new ArgumentException("type");
            }
            IList <SelectListItem> selectListItemList = (IList <SelectListItem>) new List <SelectListItem>();
            Type type1 = Nullable.GetUnderlyingType(type);

            if ((object)type1 == null)
            {
                type1 = type;
            }
            Type type2 = type1;

            if (type2 != type)
            {
                selectListItemList.Add(new SelectListItem()
                {
                    Text  = string.Empty,
                    Value = string.Empty
                });
            }
            foreach (FieldInfo field in type2.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.Public | BindingFlags.GetField))
            {
                object rawConstantValue = field.GetRawConstantValue();
                selectListItemList.Add(new SelectListItem()
                {
                    Text  = EnumHelperExtension.GetDisplayName(field),
                    Value = rawConstantValue.ToString()
                });
            }
            return(selectListItemList);
        }