MVC-HtmlHelper扩展

Select扩展:

 1 public static class SelectExtensions
 2     {
 3         /// 
 4         /// @Html.EnumDropDownListFor(model => model.PermissionType)
 5         /// 
 6         /// 
 7         /// 
 8         /// 
 9         /// 
10         /// 
11         public static MvcHtmlString EnumCustomerDropDownListFor(this HtmlHelper htmlHelper, Expression> expression) where TEnum : struct
12         {
13             return EnumCustomerDropDownListFor(htmlHelper, expression, null);
14         }
15         /// 
16         /// @Html.EnumDropDownListFor(model => model.PermissionType)
17         /// 
18         /// 
19         /// 
20         /// 
21         /// 
22         /// 
23         /// 
24         public static MvcHtmlString EnumCustomerDropDownListFor(this HtmlHelper htmlHelper, Expression> expression, object htmlAttributes) where TEnum : struct
25         {
26             return EnumCustomerDropDownListFor(htmlHelper, expression, ((IDictionary<string, object>)HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)));
27         }
28         public static MvcHtmlString EnumCustomerDropDownListFor(this HtmlHelper htmlHelper, Expression> 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");
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");
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(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(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(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($"");
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         /// 
 4         /// 使用强类型方式创建input type="radio"标签
 5         /// @this.Html.EnumRadioButtonListFor(p => p.Render)
 6         /// 
 7         /// 
 8         /// 
 9         /// 
10         /// 
11         /// 
12         public static MvcHtmlString EnumRadioButtonListFor(this HtmlHelper htmlHelper, Expression> expression) where TEnum : struct
13         {
14             return EnumRadioButtonListFor(htmlHelper, expression, null);
15         }
16         /// 
17         /// 使用强类型方式创建input type="radio"标签
18         /// @this.Html.EnumRadioButtonListFor(p => p.Render)
19         /// 
20         /// 
21         /// 
22         /// 
23         /// 
24         /// 
25         /// 
26         public static MvcHtmlString EnumRadioButtonListFor(this HtmlHelper htmlHelper, Expression> 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("" + tagBuilder.ToString() + p.GetName() + "");
46             }
47             return new MvcHtmlString(buffter.ToString());
48         }
49 
50     }

 

CheckBox扩展:

  1 public static class CheckboxExtension
  2     {
  3         /// 
  4         /// 使用强类型方式创建input type="checkbox"标签
  5         /// @Html.EnumCheckBoxListFor(model => model.PermissionType)
  6         /// 
  7         /// 
  8         /// 
  9         /// 
 10         /// 
 11         /// 
 12         public static MvcHtmlString EnumCheckBoxListFor(this HtmlHelper htmlHelper, Expression> expression) where TEnum : class
 13         {
 14             return EnumCheckBoxListFor(htmlHelper, expression, null);
 15         }
 16         /// 
 17         /// 使用强类型方式创建input type="checkbox"标签
 18         /// 
 19         /// 
 20         /// 
 21         /// 
 22         /// 
 23         /// 
 24         /// 
 25         public static MvcHtmlString EnumCheckBoxListFor(this HtmlHelper htmlHelper, Expression> 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("" + tagBuilder.ToString() + p.GetName() + "");
 61             }
 62             Array.Clear(models, 0, models.Length);
 63             models = null;
 64             return new MvcHtmlString(buffter.ToString());
 65         }
 66         /// 
 67         /// 使用弱类型方式创建input type="checkbox"标签
 68         /// @this.Html.EnumCheckBoxListFor(Model.Platforms);
 69         /// 
 70         /// 
 71         /// 
 72         /// 
 73         /// 
 74         public static IHtmlString EnumCheckBoxListFor(this HtmlHelper htmlHelper, TEnum[] enums) where TEnum : struct
 75         {
 76             return EnumCheckBoxListFor(htmlHelper, enums, null);
 77         }
 78         /// 
 79         /// 使用弱类型方式创建input type="checkbox"标签
 80         /// 
 81         /// 
 82         /// 
 83         /// 
 84         /// 
 85         /// 
 86         public static IHtmlString EnumCheckBoxListFor(this HtmlHelper htmlHelper, TEnum[] enums, IDictionary<string, object> htmlAttributes) where TEnum : struct
 87         {
 88             return htmlHelper.EnumEachFor(new Actionstring, 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("" + tagBuilder.ToString() + platform + "");
102             }));
103         }
104     }

 

你可能感兴趣的:(MVC-HtmlHelper扩展)