标签:ble models param uil view selected property eof ||
Select扩展:
1 public static class SelectExtensions 2 { 3 /// <summary> 4 /// @Html.EnumDropDownListFor(model => model.PermissionType) 5 /// </summary> 6 /// <typeparam name="TModel"></typeparam> 7 /// <typeparam name="TEnum"></typeparam> 8 /// <param name="htmlHelper"></param> 9 /// <param name="expression"></param> 10 /// <returns></returns> 11 public static MvcHtmlString EnumCustomerDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression) where TEnum : struct 12 { 13 return EnumCustomerDropDownListFor(htmlHelper, expression, null); 14 } 15 /// <summary> 16 /// @Html.EnumDropDownListFor(model => model.PermissionType) 17 /// </summary> 18 /// <typeparam name="TModel"></typeparam> 19 /// <typeparam name="TEnum"></typeparam> 20 /// <param name="htmlHelper"></param> 21 /// <param name="expression"></param> 22 /// <param name="htmlAttributes"></param> 23 /// <returns></returns> 24 public static MvcHtmlString EnumCustomerDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, object htmlAttributes) where TEnum : struct 25 { 26 return EnumCustomerDropDownListFor(htmlHelper, expression, ((IDictionary<string, object>)HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes))); 27 } 28 public static MvcHtmlString EnumCustomerDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, IDictionary<string, object> htmlAttributes) where TEnum : struct 29 { 30 ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); 31 Type enumType = HtmlExtensions.GetNonNullableModelType(metadata); 32 33 StringBuilder buffter = new StringBuilder(""); 34 if (metadata.IsNullableValueType) 35 buffter.AppendFormat("\n<option value=\"0\" selected=\"selected\">请选择...</option>"); 36 var enumItems = Enum.GetValues(enumType); 37 foreach (var enumItem in enumItems) 38 { 39 string selected = enumItem.Equals(metadata.Model) ? " selected=\"selected\"" : ""; 40 buffter.Append($"\n<option value=\"{Convert.ToInt32(enumItem)}\" {2}>{((Enum)enumItem).GetText()}</option>"); 41 } 42 43 TagBuilder tagBuilder = new TagBuilder("select") 44 { 45 InnerHtml = buffter.ToString() 46 }; 47 tagBuilder.MergeAttributes<string, object>(htmlAttributes); 48 tagBuilder.GenerateId(metadata.PropertyName); 49 tagBuilder.MergeAttribute("name", metadata.PropertyName, true); 50 return new MvcHtmlString(tagBuilder.ToString()); 51 } 52 53 public static MvcHtmlString EnumDropDownList<TEnum>(this HtmlHelper htmlHelper, TEnum tEnum, string id, string name) where TEnum : struct 54 { 55 return EnumDropDownList(htmlHelper, tEnum, id, name, null); 56 } 57 public static MvcHtmlString EnumDropDownList<TEnum>(this HtmlHelper htmlHelper, TEnum tEnum, string id, string name, object htmlAttributes) where TEnum : struct 58 { 59 return EnumDropDownList(htmlHelper, tEnum, id, name, ((IDictionary<string, object>)HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes))); 60 } 61 62 63 64 public static MvcHtmlString EnumDropDownList<TEnum>(this HtmlHelper htmlHelper, TEnum tEnum, string id, string name, IDictionary<string, object> htmlAttributes) where TEnum : struct 65 { 66 //获取所有枚举项 67 Array enumItems = Enum.GetValues(typeof(TEnum)); 68 StringBuilder buffer = new StringBuilder(); 69 string selected = string.Empty; 70 foreach (var enumItem in enumItems) 71 { 72 selected = enumItem.Equals(tEnum) ? "selected=\"selected\"" : ""; 73 buffer.Append($"<option value=\"{Convert.ToInt32(enumItem)}\" {selected}>{((Enum)enumItem).GetText()}</option>"); 74 } 75 TagBuilder tagBuilder = new TagBuilder("select") 76 { 77 InnerHtml = buffer.ToString() 78 }; 79 tagBuilder.GenerateId(id); 80 tagBuilder.MergeAttribute("name", name); 81 tagBuilder.MergeAttributes(htmlAttributes); 82 return new MvcHtmlString(tagBuilder.ToString()); 83 84 } 85 86 }
-------------------------------------------------------------------
页面调用:
@Html.EnumDropDownList(EDateType.year, "selectDateType", "selectDateType", new { @class = "c1" })
@Html.EnumCustomerDropDownListFor(p => p.DateType)//强类型
Radio扩展:
1 public static class RadioExtension 2 { 3 /// <summary> 4 /// 使用强类型方式创建input type="radio"标签 5 /// @this.Html.EnumRadioButtonListFor(p => p.Render) 6 /// </summary> 7 /// <typeparam name="TModel"></typeparam> 8 /// <typeparam name="TEnum"></typeparam> 9 /// <param name="htmlHelper"></param> 10 /// <param name="expression"></param> 11 /// <returns></returns> 12 public static MvcHtmlString EnumRadioButtonListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression) where TEnum : struct 13 { 14 return EnumRadioButtonListFor(htmlHelper, expression, null); 15 } 16 /// <summary> 17 /// 使用强类型方式创建input type="radio"标签 18 /// @this.Html.EnumRadioButtonListFor(p => p.Render) 19 /// </summary> 20 /// <typeparam name="TModel"></typeparam> 21 /// <typeparam name="TEnum"></typeparam> 22 /// <param name="htmlHelper"></param> 23 /// <param name="expression"></param> 24 /// <param name="htmlAttributes"></param> 25 /// <returns></returns> 26 public static MvcHtmlString EnumRadioButtonListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, IDictionary<string, object> htmlAttributes) where TEnum : struct 27 { 28 ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); 29 Type enumType = HtmlExtensions.GetNonNullableModelType(metadata); 30 31 StringBuilder buffter = new StringBuilder(); 32 Array enums = Enum.GetValues(enumType); 33 foreach (Enum p in enums) 34 { 35 TagBuilder tagBuilder = new TagBuilder("input"); 36 tagBuilder.GenerateId(metadata.PropertyName + p.GetValue().ToString()); 37 tagBuilder.MergeAttributes<string, object>(htmlAttributes); 38 tagBuilder.MergeAttribute("name", metadata.PropertyName, true); 39 tagBuilder.MergeAttribute("type", "radio"); 40 tagBuilder.MergeAttribute("value", p.GetValue().ToString()); 41 if (p.Equals(metadata.Model)) 42 { 43 tagBuilder.MergeAttribute("checked", "checked"); 44 } 45 buffter.AppendFormat("<label>" + tagBuilder.ToString() + p.GetName() + "</label>"); 46 } 47 return new MvcHtmlString(buffter.ToString()); 48 } 49 50 }
CheckBox扩展:
1 public static class CheckboxExtension 2 { 3 /// <summary> 4 /// 使用强类型方式创建input type="checkbox"标签 5 /// @Html.EnumCheckBoxListFor(model => model.PermissionType) 6 /// </summary> 7 /// <typeparam name="TModel"></typeparam> 8 /// <typeparam name="TEnum"></typeparam> 9 /// <param name="htmlHelper"></param> 10 /// <param name="expression"></param> 11 /// <returns></returns> 12 public static MvcHtmlString EnumCheckBoxListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression) where TEnum : class 13 { 14 return EnumCheckBoxListFor(htmlHelper, expression, null); 15 } 16 /// <summary> 17 /// 使用强类型方式创建input type="checkbox"标签 18 /// </summary> 19 /// <typeparam name="TModel"></typeparam> 20 /// <typeparam name="TEnum"></typeparam> 21 /// <param name="htmlHelper"></param> 22 /// <param name="expression"></param> 23 /// <param name="htmlAttributes"></param> 24 /// <returns></returns> 25 public static MvcHtmlString EnumCheckBoxListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, IDictionary<string, object> htmlAttributes) where TEnum : class 26 { 27 ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); 28 Type enumType = HtmlExtensions.GetNonNullableModelType(metadata); 29 if (!enumType.IsArray || !metadata.ModelType.IsArray) 30 { 31 throw new Exception("Model的属性必须为数组类型"); 32 } 33 enumType = enumType.GetElementType(); 34 if (!enumType.IsEnum) 35 { 36 throw new Exception("Model的属性必须为枚举类型的数组类型"); 37 } 38 int[] models = (int[])metadata.Model; 39 StringBuilder buffter = new StringBuilder(); 40 Array enums = Enum.GetValues(enumType); 41 foreach (string p in enums) 42 { 43 44 } 45 foreach (object p in enums) { 46 47 } 48 foreach (Enum p in enums) 49 { 50 TagBuilder tagBuilder = new TagBuilder("input"); 51 tagBuilder.GenerateId(metadata.PropertyName + p.GetValue().ToString()); 52 tagBuilder.MergeAttributes<string, object>(htmlAttributes); 53 tagBuilder.MergeAttribute("name", metadata.PropertyName, true); 54 tagBuilder.MergeAttribute("type", "checkbox"); 55 tagBuilder.MergeAttribute("value", p.GetValue().ToString()); 56 if (models.Contains(p.GetValue())) 57 { 58 tagBuilder.MergeAttribute("checked", "checked"); 59 } 60 buffter.AppendFormat("<label>" + tagBuilder.ToString() + p.GetName() + "</label>"); 61 } 62 Array.Clear(models, 0, models.Length); 63 models = null; 64 return new MvcHtmlString(buffter.ToString()); 65 } 66 /// <summary> 67 /// 使用弱类型方式创建input type="checkbox"标签 68 /// @this.Html.EnumCheckBoxListFor(Model.Platforms); 69 /// </summary> 70 /// <typeparam name="TEnum"></typeparam> 71 /// <param name="htmlHelper"></param> 72 /// <param name="enums"></param> 73 /// <returns></returns> 74 public static IHtmlString EnumCheckBoxListFor<TEnum>(this HtmlHelper htmlHelper, TEnum[] enums) where TEnum : struct 75 { 76 return EnumCheckBoxListFor<TEnum>(htmlHelper, enums, null); 77 } 78 /// <summary> 79 /// 使用弱类型方式创建input type="checkbox"标签 80 /// </summary> 81 /// <typeparam name="TEnum"></typeparam> 82 /// <param name="htmlHelper"></param> 83 /// <param name="enums"></param> 84 /// <param name="htmlAttributes"></param> 85 /// <returns></returns> 86 public static IHtmlString EnumCheckBoxListFor<TEnum>(this HtmlHelper htmlHelper, TEnum[] enums, IDictionary<string, object> htmlAttributes) where TEnum : struct 87 { 88 return htmlHelper.EnumEachFor(new Action<TEnum, string, System.Text.StringBuilder>((platform, text, sb) => 89 { 90 Type enumType = typeof(TEnum); 91 TagBuilder tagBuilder = new TagBuilder("input"); 92 tagBuilder.GenerateId(enumType.Name + Convert.ToInt32(platform).ToString()); 93 tagBuilder.MergeAttributes<string, object>(htmlAttributes); 94 tagBuilder.MergeAttribute("name", enumType.Name, true); 95 tagBuilder.MergeAttribute("type", "checkbox"); 96 tagBuilder.MergeAttribute("value", Convert.ToInt32(platform).ToString()); 97 if (enums.Contains(platform)) 98 { 99 tagBuilder.MergeAttribute("checked", "checked"); 100 } 101 sb.AppendFormat("<label>" + tagBuilder.ToString() + platform + "</label>"); 102 })); 103 } 104 }
标签:ble models param uil view selected property eof ||
原文地址:https://www.cnblogs.com/fanfan-90/p/11963686.html