コード例 #1
0
        public static MvcHtmlString EnumDropDownListFor <TModel, TProperty, TEnum>(
            this HtmlHelper <TModel> htmlHelper,
            Expression <Func <TModel, TProperty> > expression,
            TEnum selectedValue)
        {
            IEnumerable <TEnum> values = Enum.GetValues(typeof(TEnum))
                                         .Cast <TEnum>();

            IEnumerable <SelectListItem> items = from value in values
                                                 select new SelectListItem()
            {
                Text     = value.ToString(),
                Value    = value.ToString(),
                Selected = (value.Equals(selectedValue))
            };

            LocalizarDisplayNomeAtributo[] listaItens = new LocalizarDisplayNomeAtributo[values.Count()];
            LocalizarDisplayNomeAtributo   na;
            int i = 0;

            foreach (var item in items)
            {
                na = new LocalizarDisplayNomeAtributo(item.Text);
                na.NameResourceType = typeof(Mensagem);
                listaItens[i]       = na;
                i++;
            }

            i = 0;
            IEnumerable <SelectListItem> valores = from value in values
                                                   select new SelectListItem()
            {
                Text     = listaItens[i++].DisplayName,
                Value    = value.ToString(),
                Selected = (value.Equals(selectedValue))
            };

            return(SelectExtensions.DropDownListFor(htmlHelper, expression, valores));
        }
コード例 #2
0
        /// <summary>
        /// Returns an HTML select element for each property in the object that is represented
        /// by the specified expression using the specified enum items and HTML attributes.
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <typeparam name="TEnum">The type of the enum.</typeparam>
        /// <param name="htmlHelper">The HTML helper instance that this method extends.</param>
        /// <param name="expression">An expression that identifies the object that contains the properties to display.</param>
        /// <param name="htmlAttributes">An object containing a list of html sttributes to apply to the select element.</param>
        /// <returns>
        /// An HTML select element for each property in the object that is represented
        /// by the expression.
        /// </returns>
        public static MvcHtmlString EnumDropDownListForExt <TModel, TEnum>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TEnum> > expression, object htmlAttributes = null)
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);

            Type enumType               = GetNonNullableModelType(metadata);
            Type baseEnumType           = Enum.GetUnderlyingType(enumType);
            List <SelectListItem> items = new List <SelectListItem>();

            foreach (FieldInfo field in enumType.GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public))
            {
                string text     = field.Name;
                string value    = Convert.ChangeType(field.GetValue(null), baseEnumType).ToString();
                bool   selected = field.GetValue(null).Equals(metadata.Model);

                foreach (DisplayAttribute displayAttribute in field.GetCustomAttributes(true).OfType <DisplayAttribute>())
                {
                    text = displayAttribute.GetName();
                }

                items.Add(new SelectListItem()
                {
                    Text     = text,
                    Value    = value,
                    Selected = selected
                });
            }

            //if (metadata.IsNullableValueType)
            //{
            //    items.Insert(0, new SelectListItem { Text = "", Value = "" });
            //}
            items.Insert(0, new SelectListItem {
                Text = "", Value = ""
            });
            var result = items.OrderBy(x => x.Text);

            return(SelectExtensions.DropDownListFor(htmlHelper, expression, result, htmlAttributes));
        }
コード例 #3
0
        public static MvcHtmlString DropDownListWithErrorClassFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, IEnumerable <SelectListItem> selectList, object htmlAttributes, string errorClass)
        {
            MemberExpression body         = expression.Body as MemberExpression;
            string           propertyName = body.Member.Name;

            if (htmlHelper.ViewData.ModelState.IsValidField(propertyName))
            {
                return(SelectExtensions.DropDownListFor(htmlHelper, expression, selectList, htmlAttributes));
            }
            else
            {
                var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
                if (attributes.ContainsKey("class"))
                {
                    attributes["class"] += " " + errorClass;
                }
                else
                {
                    attributes.Add("class", errorClass);
                }
                return(SelectExtensions.DropDownListFor(htmlHelper, expression, selectList, attributes));
            }
        }
コード例 #4
0
        public static MvcHtmlString BootstrapDropDownFor <TModel, TValue>(this HtmlHelper <TModel> helper, Expression <Func <TModel, TValue> > expression, IList <SelectListItem> selectList)
        {
            MvcHtmlString label      = LabelExtensions.LabelFor(helper, expression, new { @class = "control-label col-md-2" });
            MvcHtmlString select     = SelectExtensions.DropDownListFor(helper, expression, selectList, new { @id = "dropdown" });
            MvcHtmlString validation = ValidationExtensions.ValidationMessageFor(helper, expression, null, new { @class = "text-danger" });
            StringBuilder innerHtml  = new StringBuilder();

            innerHtml.Append(select);
            innerHtml.Append(validation);
            TagBuilder innerDiv = new TagBuilder("div");

            innerDiv.AddCssClass("col-md-10");
            innerDiv.InnerHtml = innerHtml.ToString();
            StringBuilder outerHtml = new StringBuilder();

            outerHtml.Append(label);
            outerHtml.Append(innerDiv.ToString());
            TagBuilder outerDiv = new TagBuilder("div");

            outerDiv.AddCssClass("form-group");
            outerDiv.InnerHtml = outerHtml.ToString();
            return(MvcHtmlString.Create(outerDiv.ToString()));
        }