Example #1
0
        public static MvcHtmlString EnumDropDownListFor <TModel, TEnum>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TEnum> > expression, IDictionary <string, object> htmlAttributes) where TEnum : struct
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            Type          enumType = HtmlExtensions.GetNonNullableModelType(metadata);

            StringBuilder buffter = new StringBuilder("");

            if (metadata.IsNullableValueType)
            {
                buffter.AppendFormat("\n<option value=\"\" selected=\"selected\">请选择...</option>");
            }
            Enum.GetValues(enumType).Cast <TEnum>().ToList().ForEach(p =>
                                                                     buffter.AppendFormat("\n<option value=\"{0}\"{2}>{1}</option>",
                                                                                          Convert.ToInt32(p).ToString(),
                                                                                          HtmlExtensions.EnumDescriptionFor(p),
                                                                                          p.Equals(metadata.Model) ? " selected=\"selected\"" : ""
                                                                                          )
                                                                     );


            TagBuilder tagBuilder = new TagBuilder("select")
            {
                InnerHtml = buffter.ToString()
            };

            tagBuilder.MergeAttributes <string, object>(htmlAttributes);
            tagBuilder.GenerateId(metadata.PropertyName);
            tagBuilder.MergeAttribute("name", metadata.PropertyName, true);
            return(new MvcHtmlString(tagBuilder.ToString()));
        }
Example #2
0
        /// <summary>
        /// 使用强类型方式创建input type="checkbox"标签
        /// wangyp
        /// 2014-12-16
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TEnum"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString EnumCheckBoxListFor <TModel, TEnum>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TEnum> > expression, IDictionary <string, object> htmlAttributes) where TEnum : class
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            Type          enumType = HtmlExtensions.GetNonNullableModelType(metadata);

            if (!enumType.IsArray || !metadata.ModelType.IsArray)
            {
                throw new Exception("Model的属性必须为数组类型");
            }
            enumType = enumType.GetElementType();
            if (!enumType.IsEnum)
            {
                throw new Exception("Model的属性必须为枚举类型的数组类型");
            }
            int[]         models  = (int[])metadata.Model;
            StringBuilder buffter = new StringBuilder();
            Array         enums   = Enum.GetValues(enumType);

            foreach (Enum p in enums)
            {
                TagBuilder tagBuilder = new TagBuilder("input");
                tagBuilder.GenerateId(metadata.PropertyName + p.GetValue().ToString());
                tagBuilder.MergeAttributes <string, object>(htmlAttributes);
                tagBuilder.MergeAttribute("name", metadata.PropertyName, true);
                tagBuilder.MergeAttribute("type", "checkbox");
                tagBuilder.MergeAttribute("value", p.GetValue().ToString());
                if (models.Contains(p.GetValue()))
                {
                    tagBuilder.MergeAttribute("checked", "checked");
                }
                buffter.AppendFormat("<label>" + tagBuilder.ToString() + p.GetName() + "</label>");
            }
            Array.Clear(models, 0, models.Length);
            models = null;
            return(new MvcHtmlString(buffter.ToString()));
        }
Example #3
0
        /// <summary>
        /// 使用强类型方式创建input type="radio"标签
        /// @this.Html.EnumRadioButtonListFor(p => p.Render)
        /// wangyp
        /// 2014-12-16
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TEnum"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString EnumRadioButtonListFor <TModel, TEnum>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TEnum> > expression, IDictionary <string, object> htmlAttributes) where TEnum : struct
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            Type          enumType = HtmlExtensions.GetNonNullableModelType(metadata);

            StringBuilder buffter = new StringBuilder();
            Array         enums   = Enum.GetValues(enumType);

            foreach (Enum p in enums)
            {
                TagBuilder tagBuilder = new TagBuilder("input");
                tagBuilder.GenerateId(metadata.PropertyName + p.GetValue().ToString());
                tagBuilder.MergeAttributes <string, object>(htmlAttributes);
                tagBuilder.MergeAttribute("name", metadata.PropertyName, true);
                tagBuilder.MergeAttribute("type", "radio");
                tagBuilder.MergeAttribute("value", p.GetValue().ToString());
                if (p.Equals(metadata.Model))
                {
                    tagBuilder.MergeAttribute("checked", "checked");
                }
                buffter.AppendFormat("<label>" + tagBuilder.ToString() + p.GetName() + "</label>");
            }
            return(new MvcHtmlString(buffter.ToString()));
        }