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())); }
public static MvcHtmlString EnumDropDownList <TEnum>(this HtmlHelper htmlHelper, TEnum tEnum, string id, string name, IDictionary <string, object> htmlAttributes) where TEnum : struct { var enums = Enum.GetValues(tEnum.GetType()).Cast <TEnum>().ToArray(); StringBuilder buffter = new StringBuilder(""); Array.ForEach(enums, p => buffter.AppendFormat("\n<option value=\"{0}\"{2}>{1}</option>", Convert.ToInt32(p).ToString(), HtmlExtensions.EnumDescriptionFor(p), p.Equals(tEnum) ? " selected=\"selected\"" : "" ) ); TagBuilder tagBuilder = new TagBuilder("select") { InnerHtml = buffter.ToString() }; tagBuilder.MergeAttributes <string, object>(htmlAttributes); tagBuilder.GenerateId(id); tagBuilder.MergeAttribute("name", name, true); return(new MvcHtmlString(tagBuilder.ToString())); }
/// <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())); }
/// <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())); }
public static IHtmlString EnumFilterData(Type type, Array enums, string firstName, int firstValue, bool isConvertValue = true) { if (!type.IsEnum) { throw new ArgumentException(type.FullName + "不是枚举类型!", "type"); } StringBuilder builder = new StringBuilder(""); builder.Append("["); if (firstName == null) { foreach (var value in enums) { builder.Append("{\"text\":\"" + (isConvertValue ? value.ToString() : HtmlExtensions.EnumDescriptionFor(value)) + "\",\"value\":" + Convert.ToInt32(value) + "},"); } if (enums.Length > 0) { builder = builder.Remove(builder.Length - 1, 1); } } else { builder.Append("{\"text\":\"" + firstName + "\",\"value\":" + firstValue + "}"); foreach (var value in enums) { builder.Append(",{\"text\":\"" + (isConvertValue ? value.ToString() : HtmlExtensions.EnumDescriptionFor(value)) + "\",\"value\":" + Convert.ToInt32(value) + "}"); } } builder.Append("]"); return(new HtmlString(builder.ToString())); }
public static IHtmlString EnumFormatter(Type type, Array enums, bool isConvertValue = true) { if (!type.IsEnum) { throw new ArgumentException(type.FullName + "不是枚举类型!", "type"); } StringBuilder builder = new StringBuilder(""); builder.Append("function(value,row,index){switch(value){"); foreach (var value in enums) { builder.Append("case " + Convert.ToInt32(value) + ":return \"" + (isConvertValue ? value.ToString() : HtmlExtensions.EnumDescriptionFor(value)) + "\";"); } builder.Append("}return \"\";}");//} return(new HtmlString(builder.ToString())); }