ASP.NET MVC 3 扩展生成 HTML 的 Input 元素
直接贴代码了:
using System;using System.Collections.Generic;using System.Data.Linq;using System.Globalization;using System.Linq.Expressions;using System.Text;using System.Web.Mvc;using System.Web.Mvc.Html;using System.Web.Mvc.Properties;using System.Web.Routing;namespace JH.ServiceFacade.Mvc.HtmlExtensions{ public static class MvcInputExtensions { ///通过使用指定的 HTML 帮助器和窗体字段名称,返回复选框 input 元素。 ///一个 input 元素,其 type 特性设置为“checkbox”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称。 public static MvcHtmlString CheckBox(this HtmlHelper htmlHelper, string name) { return htmlHelper.CheckBox(name, (object) null); } ///通过使用指定的 HTML 帮助器、窗体字段名称以及一个用于指示是否已选中复选框的值,返回复选框 input 元素。 ///一个 input 元素,其 type 特性设置为“checkbox”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称。 /// 如果要选中复选框,则为 true;否则为 false。按此顺序检索复选框的值:对象、此参数的值、 对象,最后是 html 特性中的 checked 特性。 public static MvcHtmlString CheckBox(this HtmlHelper htmlHelper, string name, bool isChecked) { return htmlHelper.CheckBox(name, isChecked, (object) null); } /// 通过使用指定的 HTML 帮助器、窗体字段的名称、用于指示是否已选中复选框的值以及 HTML 特性,返回复选框 input 元素。 ///一个 input 元素,其 type 特性设置为“checkbox”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称。 /// 如果要选中复选框,则为 true;否则为 false。按此顺序检索复选框的值:对象、此参数的值、 对象,最后是 html 特性中的 checked 特性。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 public static MvcHtmlString CheckBox(this HtmlHelper htmlHelper, string name, bool isChecked, object htmlAttributes) { return MvcInputExtensions.CheckBox(htmlHelper, name, isChecked, (IDictionary ) HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } /// 通过使用指定的 HTML 帮助器、窗体字段的名称和 HTML 特性,返回复选框 input 元素。 ///一个 input 元素,其 type 特性设置为“checkbox”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 public static MvcHtmlString CheckBox(this HtmlHelper htmlHelper, string name, object htmlAttributes) { return MvcInputExtensions.CheckBox(htmlHelper, name, (IDictionary) HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } /// 通过使用指定的 HTML 帮助器、窗体字段的名称和 HTML 特性,返回复选框 input 元素。 ///一个 input 元素,其 type 特性设置为“checkbox”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 public static MvcHtmlString CheckBox(this HtmlHelper htmlHelper, string name, IDictionaryhtmlAttributes) { return MvcInputExtensions.CheckBoxHelper(htmlHelper, (ModelMetadata) null, name, new bool?(), htmlAttributes); } /// 通过使用指定的 HTML 帮助器、窗体字段的名称、用于指示是否已选中复选框的值以及 HTML 特性,返回复选框 input 元素。 ///一个 input 元素,其 type 特性设置为“checkbox”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称。 /// 如果要选中复选框,则为 true;否则为 false。按此顺序检索复选框的值:对象、此参数的值、 对象,最后是 html 特性中的 checked 特性。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 public static MvcHtmlString CheckBox(this HtmlHelper htmlHelper, string name, bool isChecked, IDictionary htmlAttributes) { return MvcInputExtensions.CheckBoxHelper(htmlHelper, (ModelMetadata) null, name, new bool?(isChecked), htmlAttributes); } /// 为指定表达式表示的对象中的每个属性返回对应的复选框 input 元素。 ///一个 HTML input 元素,其 type 特性针对指定表达式表示的对象中的每个属性均设置为“checkbox”。 /// 此方法扩展的 HTML 帮助器实例。 /// 一个表达式,用于标识包含要呈现的属性的对象。 ///模型的类型。 ////// public static MvcHtmlString CheckBoxFor参数为 null。 (this HtmlHelper htmlHelper, Expression > expression) { return MvcInputExtensions.CheckBoxFor (htmlHelper, expression, (IDictionary ) null); } /// 使用指定的 HTML 特性,为由指定表达式表示的对象中的每个属性返回对应的复选框 input 元素。 ///一个 HTML input 元素,其 type 特性已使用指定的 HTML 特性,针对指定表达式表示的对象中的每个属性均设置为“checkbox”。 /// 此方法扩展的 HTML 帮助器实例。 /// 一个表达式,用于标识包含要呈现的属性的对象。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 ///模型的类型。 ////// public static MvcHtmlString CheckBoxFor参数为 null。 (this HtmlHelper htmlHelper, Expression > expression, object htmlAttributes) { return MvcInputExtensions.CheckBoxFor (htmlHelper, expression, (IDictionary ) HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } /// 使用指定的 HTML 特性,为由指定表达式表示的对象中的每个属性返回对应的复选框 input 元素。 ///一个 HTML input 元素,其 type 特性已使用指定的 HTML 特性,针对指定表达式表示的对象中的每个属性均设置为“checkbox”。 /// 此方法扩展的 HTML 帮助器实例。 /// 一个表达式,用于标识包含要呈现的属性的对象。 /// 一个包含要为该元素设置的 HTML 特性的字典。 ///模型的类型。 ////// public static MvcHtmlString CheckBoxFor参数为 null。 (this HtmlHelper htmlHelper, Expression > expression, IDictionary htmlAttributes) { if (expression == null) throw new ArgumentNullException("expression"); ModelMetadata metadata = ModelMetadata.FromLambdaExpression (expression, htmlHelper.ViewData); bool? isChecked = new bool?(); bool result; if (metadata.Model != null && bool.TryParse(metadata.Model.ToString(), out result)) isChecked = new bool?(result); return MvcInputExtensions.CheckBoxHelper((HtmlHelper) htmlHelper, metadata, ExpressionHelper.GetExpressionText((LambdaExpression) expression), isChecked, htmlAttributes); } private static MvcHtmlString CheckBoxHelper(HtmlHelper htmlHelper, ModelMetadata metadata, string name, bool? isChecked, IDictionary htmlAttributes) { RouteValueDictionary routeValueDictionary1 = MvcInputExtensions.ToRouteValueDictionary(htmlAttributes); bool hasValue = isChecked.HasValue; if (hasValue) routeValueDictionary1.Remove("checked"); HtmlHelper htmlHelper1 = htmlHelper; int num1 = 0; ModelMetadata metadata1 = metadata; string name1 = name; string str = "true"; int num2 = !hasValue ? 1 : 0; bool? nullable = isChecked; int num3 = nullable.HasValue ? (nullable.GetValueOrDefault() ? 1 : 0) : 0; int num4 = 1; int num5 = 0; // ISSUE: variable of the null type //__Null local = null; RouteValueDictionary routeValueDictionary2 = routeValueDictionary1; return MvcInputExtensions.InputHelper(htmlHelper1, (InputType) num1, metadata1, name1, (object) str, num2 != 0, num3 != 0, num4 != 0, num5 != 0, (string) null, (IDictionary ) routeValueDictionary2); } /// 通过使用指定的 HTML 帮助器和窗体字段的名称,返回隐藏的 input 元素。 ///一个 input 元素,其 type 特性设置为“hidden”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称和用于查找值的键。 public static MvcHtmlString Hidden(this HtmlHelper htmlHelper, string name) { return MvcInputExtensions.Hidden(htmlHelper, name, (object) null, (IDictionary ) null); } /// 通过使用指定的 HTML 帮助器、窗体字段的名称和值,返回隐藏的 input 元素。 ///一个 input 元素,其 type 特性设置为“hidden”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称和用于查找值的键。 /// 隐藏的 input 元素的值。按此顺序检索值: 对象、此参数的值、 对象,最后是 html 特性中的 value 特性。 public static MvcHtmlString Hidden(this HtmlHelper htmlHelper, string name, object value) { return MvcInputExtensions.Hidden(htmlHelper, name, value, (IDictionary ) null); } /// 通过使用指定的 HTML 帮助器、窗体字段的名称、值和 HTML 特性,返回隐藏的 input 元素。 ///一个 input 元素,其 type 特性设置为“hidden”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称和用于查找值的键。 /// 隐藏的 input 元素的值。按此顺序检索值: 对象、此参数的值、 对象,最后是 html 特性中的 value 特性。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 public static MvcHtmlString Hidden(this HtmlHelper htmlHelper, string name, object value, object htmlAttributes) { return MvcInputExtensions.Hidden(htmlHelper, name, value, (IDictionary ) HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } /// 通过使用指定的 HTML 帮助器、窗体字段的名称、值和 HTML 特性,返回隐藏的 input 元素。 ///一个 input 元素,其 type 特性设置为“hidden”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称和用于查找值的键。 /// 隐藏的 input 元素的值。按此顺序检索值: 对象、此参数的值、 对象,最后是 html 特性中的 value 特性。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 public static MvcHtmlString Hidden(this HtmlHelper htmlHelper, string name, object value, IDictionary htmlAttributes) { return MvcInputExtensions.HiddenHelper(htmlHelper, (ModelMetadata) null, value, value == null, name, htmlAttributes); } /// 为由指定表达式表示的对象中的每个属性返回对应的 HTML 隐藏 input 元素。 ///一个 input 元素,其 type 特性针对表达式表示的对象中的每个属性均设置为“hidden”。 /// 此方法扩展的 HTML 帮助器实例。 /// 一个表达式,用于标识包含要呈现的属性的对象。 ///模型的类型。 ///属性的类型。 public static MvcHtmlString HiddenFor(this HtmlHelper htmlHelper, Expression > expression) { return MvcInputExtensions.HiddenFor (htmlHelper, expression, (IDictionary ) null); } /// 使用指定的 HTML 特性,为由指定表达式表示的对象中的每个属性返回对应的 HTML 隐藏 input 元素。 ///一个 input 元素,其 type 特性针对表达式表示的对象中的每个属性均设置为“hidden”。 /// 此方法扩展的 HTML 帮助器实例。 /// 一个表达式,用于标识包含要呈现的属性的对象。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 ///模型的类型。 ///属性的类型。 public static MvcHtmlString HiddenFor(this HtmlHelper htmlHelper, Expression > expression, object htmlAttributes) { return MvcInputExtensions.HiddenFor (htmlHelper, expression, (IDictionary ) HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } /// 使用指定的 HTML 特性,为由指定表达式表示的对象中的每个属性返回对应的 HTML 隐藏 input 元素。 ///一个 input 元素,其 type 特性针对表达式表示的对象中的每个属性均设置为“hidden”。 /// 此方法扩展的 HTML 帮助器实例。 /// 一个表达式,用于标识包含要呈现的属性的对象。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 ///模型的类型。 ///属性的类型。 public static MvcHtmlString HiddenFor(this HtmlHelper htmlHelper, Expression > expression, IDictionary htmlAttributes) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression (expression, htmlHelper.ViewData); return MvcInputExtensions.HiddenHelper((HtmlHelper) htmlHelper, metadata, metadata.Model, false, ExpressionHelper.GetExpressionText((LambdaExpression) expression), htmlAttributes); } private static MvcHtmlString HiddenHelper(HtmlHelper htmlHelper, ModelMetadata metadata, object value, bool useViewData, string expression, IDictionary htmlAttributes) { Binary binary = value as Binary; if (binary != (Binary) null) value = (object) binary.ToArray(); byte[] inArray = value as byte[]; if (inArray != null) value = (object) Convert.ToBase64String(inArray); return MvcInputExtensions.InputHelper(htmlHelper, InputType.Hidden, metadata, expression, value, useViewData, false, true, true, (string) null, htmlAttributes); } /// 通过使用指定的 HTML 帮助器和窗体字段的名称,返回密码 input 元素。 ///一个 input 元素,其 type 特性设置为“password”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称和用于查找值的键。 public static MvcHtmlString Password(this HtmlHelper htmlHelper, string name) { return htmlHelper.Password(name, (object) null); } /// 通过使用指定的 HTML 帮助器、窗体字段的名称和值,返回密码 input 元素。 ///一个 input 元素,其 type 特性设置为“password”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称和用于查找值的键。 /// 密码 input 元素的值。如果未提供此参数的值,则使用 html 特性中的 value 特性来检索值。 public static MvcHtmlString Password(this HtmlHelper htmlHelper, string name, object value) { return MvcInputExtensions.Password(htmlHelper, name, value, (IDictionary ) null); } /// 通过使用指定的 HTML 帮助器、窗体字段的名称、值和 HTML 特性,返回密码 input 元素。 ///一个 input 元素,其 type 特性设置为“password”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称和用于查找值的键。 /// 密码 input 元素的值。如果未提供此参数的值,则使用 html 特性中的 value 特性来检索值。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 public static MvcHtmlString Password(this HtmlHelper htmlHelper, string name, object value, object htmlAttributes) { return MvcInputExtensions.Password(htmlHelper, name, value, (IDictionary ) HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } /// 通过使用指定的 HTML 帮助器、窗体字段的名称、值和 HTML 特性,返回密码 input 元素。 ///一个 input 元素,其 type 特性设置为“password”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称和用于查找值的键。 /// 密码 input 元素的值。如果未提供此参数的值,则使用 html 特性中的 value 特性来检索值。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 public static MvcHtmlString Password(this HtmlHelper htmlHelper, string name, object value, IDictionary htmlAttributes) { return MvcInputExtensions.PasswordHelper(htmlHelper, (ModelMetadata) null, name, value, htmlAttributes); } /// 为由指定表达式表示的对象中的每个属性返回对应的密码 input 元素。 ///一个 HTML input 元素,其 type 特性针对指定表达式表示的对象中的每个属性均设置为“password”。 /// 此方法扩展的 HTML 帮助器实例。 /// 一个表达式,用于标识包含要呈现的属性的对象。 ///模型的类型。 ///值的类型。 ////// public static MvcHtmlString PasswordFor参数为 null。 (this HtmlHelper htmlHelper, Expression > expression) { return MvcInputExtensions.PasswordFor (htmlHelper, expression, (IDictionary ) null); } /// 使用指定的 HTML 特性,为由指定表达式表示的对象中的每个属性返回对应的密码 input 元素。 ///一个 HTML input 元素,其 type 特性已使用指定的 HTML 特性,针对指定表达式表示的对象中的每个属性均设置为“password”。 /// 此方法扩展的 HTML 帮助器实例。 /// 一个表达式,用于标识包含要呈现的属性的对象。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 ///模型的类型。 ///值的类型。 ////// public static MvcHtmlString PasswordFor参数为 null。 (this HtmlHelper htmlHelper, Expression > expression, object htmlAttributes) { return MvcInputExtensions.PasswordFor (htmlHelper, expression, (IDictionary ) HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } /// 使用指定的 HTML 特性,为由指定表达式表示的对象中的每个属性返回对应的密码 input 元素。 ///一个 HTML input 元素,其 type 特性已使用指定的 HTML 特性,针对指定表达式表示的对象中的每个属性均设置为“password”。 /// 此方法扩展的 HTML 帮助器实例。 /// 一个表达式,用于标识包含要呈现的属性的对象。 /// 一个包含要为该元素设置的 HTML 特性的字典。 ///模型的类型。 ///值的类型。 ////// public static MvcHtmlString PasswordFor参数为 null。 (this HtmlHelper htmlHelper, Expression > expression, IDictionary htmlAttributes) { if (expression == null) throw new ArgumentNullException("expression"); return MvcInputExtensions.PasswordHelper((HtmlHelper) htmlHelper, ModelMetadata.FromLambdaExpression (expression, htmlHelper.ViewData), ExpressionHelper.GetExpressionText((LambdaExpression) expression), (object) null, htmlAttributes); } private static MvcHtmlString PasswordHelper(HtmlHelper htmlHelper, ModelMetadata metadata, string name, object value, IDictionary htmlAttributes) { return MvcInputExtensions.InputHelper(htmlHelper, InputType.Password, metadata, name, value, false, false, true, true, (string) null, htmlAttributes); } /// 返回用于呈现互斥选项的单选按钮 input 元素。 ///一个 input 元素,其 type 特性设置为“radio”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称和用于查找值的键。 /// 所选单选按钮的值。按此顺序检索值: 对象、此参数的值、 对象,最后是 html 特性中的 value 特性。 /// /// ///参数为 null 或为空。 /// public static MvcHtmlString RadioButton(this HtmlHelper htmlHelper, string name, object value) { return htmlHelper.RadioButton(name, value, (object) null); } ///参数为 null。 返回用于呈现互斥选项的单选按钮 input 元素。 ///一个 input 元素,其 type 特性设置为“radio”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称和用于查找值的键。 /// 所选单选按钮的值。按此顺序检索值: 对象、此参数的值、 对象,最后是 html 特性中的 value 特性。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 /// /// ///参数为 null 或为空。 /// public static MvcHtmlString RadioButton(this HtmlHelper htmlHelper, string name, object value, object htmlAttributes) { return MvcInputExtensions.RadioButton(htmlHelper, name, value, (IDictionary参数为 null。 ) HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } /// 返回用于呈现互斥选项的单选按钮 input 元素。 ///一个 input 元素,其 type 特性设置为“radio”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称和用于查找值的键。 /// 所选单选按钮的值。按此顺序检索值: 对象、此参数的值、 对象,最后是 html 特性中的 value 特性。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 /// /// ///参数为 null 或为空。 /// public static MvcHtmlString RadioButton(this HtmlHelper htmlHelper, string name, object value, IDictionary参数为 null。 htmlAttributes) { string b = Convert.ToString(value, (IFormatProvider) CultureInfo.CurrentCulture); bool isChecked = !string.IsNullOrEmpty(name) && string.Equals(EvalString(htmlHelper.ViewData, name), b, StringComparison.OrdinalIgnoreCase); RouteValueDictionary routeValueDictionary = MvcInputExtensions.ToRouteValueDictionary(htmlAttributes); if (routeValueDictionary.ContainsKey("checked")) return MvcInputExtensions.InputHelper(htmlHelper, InputType.Radio, (ModelMetadata) null, name, value, false, false, true, true, (string) null, (IDictionary ) routeValueDictionary); return MvcInputExtensions.RadioButton(htmlHelper, name, value, isChecked, htmlAttributes); } /// 返回用于呈现互斥选项的单选按钮 input 元素。 ///一个 input 元素,其 type 特性设置为“radio”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称和用于查找值的键。 /// 所选单选按钮的值。按此顺序检索值: 对象、此参数的值、 对象,最后是 html 特性中的 value 特性。 /// 如果要选中单选按钮,则为 true;否则为 false。 /// /// ///参数为 null 或为空。 /// public static MvcHtmlString RadioButton(this HtmlHelper htmlHelper, string name, object value, bool isChecked) { return htmlHelper.RadioButton(name, value, isChecked, (object) null); } ///参数为 null。 返回用于呈现互斥选项的单选按钮 input 元素。 ///一个 input 元素,其 type 特性设置为“radio”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称和用于查找值的键。 /// 所选单选按钮的值。按此顺序检索值: 对象、此参数的值、 对象,最后是 html 特性中的 value 特性。 /// 如果要选中单选按钮,则为 true;否则为 false。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 /// /// ///参数为 null 或为空。 /// public static MvcHtmlString RadioButton(this HtmlHelper htmlHelper, string name, object value, bool isChecked, object htmlAttributes) { return MvcInputExtensions.RadioButton(htmlHelper, name, value, isChecked, (IDictionary参数为 null。 ) HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } /// 返回用于呈现互斥选项的单选按钮 input 元素。 ///一个 input 元素,其 type 特性设置为“radio”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称和用于查找值的键。 /// 所选单选按钮的值。按此顺序检索值: 对象、此参数的值、 对象,最后是 html 特性中的 value 特性。 /// 如果要选中单选按钮,则为 true;否则为 false。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 /// /// ///参数为 null 或为空。 /// public static MvcHtmlString RadioButton(this HtmlHelper htmlHelper, string name, object value, bool isChecked, IDictionary参数为 null。 htmlAttributes) { if (value == null) throw new ArgumentNullException("value"); RouteValueDictionary routeValueDictionary = MvcInputExtensions.ToRouteValueDictionary(htmlAttributes); routeValueDictionary.Remove("checked"); return MvcInputExtensions.InputHelper(htmlHelper, InputType.Radio, (ModelMetadata) null, name, value, false, isChecked, true, true, (string) null, (IDictionary ) routeValueDictionary); } /// 为指定表达式表示的对象中的每个属性返回对应的单选按钮 input 元素。 ///一个 HTML input 元素,其 type 特性针对指定表达式表示的对象中的每个属性均设置为“radio”。 /// 此方法扩展的 HTML 帮助器实例。 /// 一个表达式,用于标识包含要呈现的属性的对象。 /// 所选单选按钮的值。按此顺序检索值:对象、此参数的值、 对象,最后是 html 特性中的 value 特性。 /// 模型的类型。 ///值的类型。 ////// public static MvcHtmlString RadioButtonFor参数为 null。 (this HtmlHelper htmlHelper, Expression > expression, object value) { return MvcInputExtensions.RadioButtonFor (htmlHelper, expression, value, (IDictionary ) null); } /// 使用指定的 HTML 特性,为由指定表达式表示的对象中的每个属性返回对应的单选按钮 input 元素。 ///一个 HTML input 元素,其 type 特性已使用指定的 HTML 特性,针对指定表达式表示的对象中的每个属性均设置为“radio”。 /// 此方法扩展的 HTML 帮助器实例。 /// 一个表达式,用于标识包含要呈现的属性的对象。 /// 所选单选按钮的值。按此顺序检索值:对象、此参数的值、 对象,最后是 html 特性中的 value 特性。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 /// 模型的类型。 ///值的类型。 ////// public static MvcHtmlString RadioButtonFor参数为 null。 (this HtmlHelper htmlHelper, Expression > expression, object value, object htmlAttributes) { return MvcInputExtensions.RadioButtonFor (htmlHelper, expression, value, (IDictionary ) HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } /// 使用指定的 HTML 特性,为由指定表达式表示的对象中的每个属性返回对应的单选按钮 input 元素。 ///一个 HTML input 元素,其 type 特性已使用指定的 HTML 特性,针对指定表达式表示的对象中的每个属性均设置为“radio”。 /// 此方法扩展的 HTML 帮助器实例。 /// 一个表达式,用于标识包含要呈现的属性的对象。 /// 所选单选按钮的值。按此顺序检索值:对象、此参数的值、 对象,最后是 html 特性中的 value 特性。 /// 一个包含要为该元素设置的 HTML 特性的字典。 /// 模型的类型。 ///值的类型。 ////// public static MvcHtmlString RadioButtonFor参数为 null。 (this HtmlHelper htmlHelper, Expression > expression, object value, IDictionary htmlAttributes) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression (expression, htmlHelper.ViewData); return MvcInputExtensions.RadioButtonHelper((HtmlHelper) htmlHelper, metadata, metadata.Model, ExpressionHelper.GetExpressionText((LambdaExpression) expression), value, new bool?(), htmlAttributes); } private static MvcHtmlString RadioButtonHelper(HtmlHelper htmlHelper, ModelMetadata metadata, object model, string name, object value, bool? isChecked, IDictionary htmlAttributes) { if (value == null) throw new ArgumentNullException("value"); RouteValueDictionary routeValueDictionary1 = MvcInputExtensions.ToRouteValueDictionary(htmlAttributes); if (isChecked.HasValue) { routeValueDictionary1.Remove("checked"); } else { string b = Convert.ToString(value, (IFormatProvider) CultureInfo.CurrentCulture); isChecked = new bool?(model != null && !string.IsNullOrEmpty(name) && string.Equals(model.ToString(), b, StringComparison.OrdinalIgnoreCase)); } HtmlHelper htmlHelper1 = htmlHelper; int num1 = 3; ModelMetadata metadata1 = metadata; string name1 = name; object obj = value; int num2 = 0; bool? nullable = isChecked; int num3 = nullable.HasValue ? (nullable.GetValueOrDefault() ? 1 : 0) : 0; int num4 = 1; int num5 = 1; // ISSUE: variable of the null type //__Null local = null; RouteValueDictionary routeValueDictionary2 = routeValueDictionary1; return MvcInputExtensions.InputHelper(htmlHelper1, (InputType) num1, metadata1, name1, obj, num2 != 0, num3 != 0, num4 != 0, num5 != 0, (string) null, (IDictionary ) routeValueDictionary2); } /// 通过使用指定的 HTML 帮助器和窗体字段的名称,返回文本 input 元素。 ///一个 input 元素,其 type 特性设置为“text”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称和用于查找值的键。 public static MvcHtmlString TextBox(this HtmlHelper htmlHelper, string name) { return htmlHelper.TextBox(name, (object) null); } /// 通过使用指定的 HTML 帮助器、窗体字段的名称和值,返回文本 input 元素。 ///一个 input 元素,其 type 特性设置为“text”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称和用于查找值的键。 /// 文本 input 元素的值。按此顺序检索值: 对象、此参数的值、 对象,最后是 html 特性中的 value 特性。 public static MvcHtmlString TextBox(this HtmlHelper htmlHelper, string name, object value) { return MvcInputExtensions.TextBox(htmlHelper, name, value, (string) null); } /// 返回文本 input 元素。 ///一个 input 元素,其 type 特性设置为“text”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称。 /// 文本 input 元素的值。按此顺序检索值:对象、此参数的值、 对象,最后是 html 特性中的 value 特性。 /// 用于设置输入格式的字符串。 public static MvcHtmlString TextBox(this HtmlHelper htmlHelper, string name, object value, string format) { return htmlHelper.TextBox(name, value, format, (object) null); } /// 通过使用指定的 HTML 帮助器、窗体字段的名称、值和 HTML 特性,返回文本 input 元素。 ///一个 input 元素,其 type 特性设置为“text”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称和用于查找值的键。 /// 文本 input 元素的值。按此顺序检索值: 对象、此参数的值、 对象,最后是 html 特性中的 value 特性。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 public static MvcHtmlString TextBox(this HtmlHelper htmlHelper, string name, object value, object htmlAttributes) { return htmlHelper.TextBox(name, value, (string) null, htmlAttributes); } /// 返回文本 input 元素。 ///一个 input 元素,其 type 特性设置为“text”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称和用于查找值的键。 /// 文本 input 元素的值。按此顺序检索值: 对象、此参数的值、 对象,最后是 html 特性中的 value 特性。 /// 用于设置输入格式的字符串。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 public static MvcHtmlString TextBox(this HtmlHelper htmlHelper, string name, object value, string format, object htmlAttributes) { return MvcInputExtensions.TextBox(htmlHelper, name, value, format, (IDictionary ) HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } /// 通过使用指定的 HTML 帮助器、窗体字段的名称、值和 HTML 特性,返回文本 input 元素。 ///一个 input 元素,其 type 特性设置为“text”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称和用于查找值的键。 /// 文本 input 元素的值。按此顺序检索值: 对象、此参数的值、 对象,最后是 html 特性中的 value 特性。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 public static MvcHtmlString TextBox(this HtmlHelper htmlHelper, string name, object value, IDictionary htmlAttributes) { return MvcInputExtensions.TextBox(htmlHelper, name, value, (string) null, htmlAttributes); } /// 返回文本 input 元素。 ///一个 input 元素,其 type 特性设置为“text”。 /// 此方法扩展的 HTML 帮助器实例。 /// 窗体字段的名称和用于查找值的键。 /// 文本 input 元素的值。按此顺序检索值: 对象、此参数的值、 对象,最后是 html 特性中的 value 特性。 /// 用于设置输入格式的字符串。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 public static MvcHtmlString TextBox(this HtmlHelper htmlHelper, string name, object value, string format, IDictionary htmlAttributes) { return MvcInputExtensions.InputHelper(htmlHelper, InputType.Text, (ModelMetadata) null, name, value, value == null, false, true, true, format, htmlAttributes); } /// 为由指定表达式表示的对象中的每个属性返回对应的文本 input 元素。 ///一个 HTML input 元素,其 type 特性针对表达式表示的对象中的每个属性均设置为“text”。 /// 此方法扩展的 HTML 帮助器实例。 /// 一个表达式,用于标识包含要呈现的属性的对象。 ///模型的类型。 ///值的类型。 ////// public static MvcHtmlString TextBoxFor参数为 null 或为空。 (this HtmlHelper htmlHelper, Expression > expression) { return MvcInputExtensions.TextBoxFor (htmlHelper, expression, (string) null); } /// 返回文本 input 元素。 ///一个 input 元素,其 type 特性设置为“text”。 /// 此方法扩展的 HTML 帮助器实例。 /// 一个表达式,用于标识包含要显示的属性的对象。 /// 用于设置输入格式的字符串。 ///模型的类型。 ///值的类型。 public static MvcHtmlString TextBoxFor(this HtmlHelper htmlHelper, Expression > expression, string format) { return MvcInputExtensions.TextBoxFor (htmlHelper, expression, format, (IDictionary ) null); } /// 使用指定的 HTML 特性,为由指定表达式表示的对象中的每个属性返回对应的文本 input 元素。 ///一个 HTML input 元素,其 type 特性针对表达式表示的对象中的每个属性均设置为“text”。 /// 此方法扩展的 HTML 帮助器实例。 /// 一个表达式,用于标识包含要呈现的属性的对象。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 ///模型的类型。 ///值的类型。 ////// public static MvcHtmlString TextBoxFor参数为 null 或为空。 (this HtmlHelper htmlHelper, Expression > expression, object htmlAttributes) { return htmlHelper.TextBoxFor (expression, (string) null, htmlAttributes); } /// 返回文本 input 元素。 ///一个 input 元素,其 type 特性设置为“text”。 /// 此方法扩展的 HTML 帮助器实例。 /// 一个表达式,用于标识包含要显示的属性的对象。 /// 用于设置输入格式的字符串。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 ///模型的类型。 ///值的类型。 public static MvcHtmlString TextBoxFor(this HtmlHelper htmlHelper, Expression > expression, string format, object htmlAttributes) { return MvcInputExtensions.TextBoxFor (htmlHelper, expression, format, (IDictionary ) HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } /// 使用指定的 HTML 特性,为由指定表达式表示的对象中的每个属性返回对应的文本 input 元素。 ///一个 HTML input 元素,其 type 特性针对表达式表示的对象中的每个属性均设置为“text”。 /// 此方法扩展的 HTML 帮助器实例。 /// 一个表达式,用于标识包含要呈现的属性的对象。 /// 一个包含要为该元素设置的 HTML 特性的字典。 ///模型的类型。 ///值的类型。 ////// public static MvcHtmlString TextBoxFor参数为 null 或为空。 (this HtmlHelper htmlHelper, Expression > expression, IDictionary htmlAttributes) { return MvcInputExtensions.TextBoxFor (htmlHelper, expression, (string) null, htmlAttributes); } /// 返回文本 input 元素。 ///一个 input 元素,其 type 特性设置为“text”。 /// 此方法扩展的 HTML 帮助器实例。 /// 一个表达式,用于标识包含要显示的属性的对象。 /// 用于设置输入格式的字符串。 /// 一个对象,其中包含要为该元素设置的 HTML 特性。 ///模型的类型。 ///值的类型。 public static MvcHtmlString TextBoxFor(this HtmlHelper htmlHelper, Expression > expression, string format, IDictionary htmlAttributes) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression (expression, htmlHelper.ViewData); return htmlHelper.TextBoxHelper(metadata, metadata.Model, ExpressionHelper.GetExpressionText((LambdaExpression) expression), format, htmlAttributes); } private static MvcHtmlString TextBoxHelper(this HtmlHelper htmlHelper, ModelMetadata metadata, object model, string expression, string format, IDictionary htmlAttributes) { return InputHelper(htmlHelper, InputType.Text, metadata, expression, model, false, false, true, true, format, htmlAttributes); } private static MvcHtmlString InputHelper(HtmlHelper htmlHelper, InputType inputType, ModelMetadata metadata, string name, object value, bool useViewData, bool isChecked, bool setId, bool isExplicitValue, string format, IDictionary htmlAttributes) { string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name); if (string.IsNullOrEmpty(fullHtmlFieldName)) throw new ArgumentException("Value cannot be null or empty.", "name"); TagBuilder tagBuilder1 = new TagBuilder("input"); tagBuilder1.MergeAttributes (htmlAttributes); tagBuilder1.MergeAttribute("type", HtmlHelper.GetInputTypeString(inputType)); tagBuilder1.MergeAttribute("name", fullHtmlFieldName, true); string b = htmlHelper.FormatValue(value, format); bool flag = false; switch (inputType) { case InputType.CheckBox: bool? modelStateValue1 = GetModelStateValue(htmlHelper.ViewData,fullHtmlFieldName, typeof (bool)) as bool?; if (modelStateValue1.HasValue) { isChecked = modelStateValue1.Value; flag = true; goto case InputType.Radio; } else goto case InputType.Radio; case InputType.Password: if (value != null) { tagBuilder1.MergeAttribute("value", b, isExplicitValue); break; } break; case InputType.Radio: if (!flag) { string modelStateValue2 = GetModelStateValue(htmlHelper.ViewData,fullHtmlFieldName, typeof (string)) as string; if (modelStateValue2 != null) { isChecked = string.Equals(modelStateValue2, b, StringComparison.Ordinal); flag = true; } } if (!flag && useViewData) isChecked = EvalBoolean(htmlHelper.ViewData, fullHtmlFieldName); if (isChecked) tagBuilder1.MergeAttribute("checked", "checked"); tagBuilder1.MergeAttribute("value", b, isExplicitValue); break; default: string modelStateValue3 = (string) GetModelStateValue(htmlHelper.ViewData, fullHtmlFieldName, typeof (string)); tagBuilder1.MergeAttribute("value", modelStateValue3 ?? (useViewData ? EvalString(htmlHelper.ViewData, fullHtmlFieldName, format) : b), isExplicitValue); break; } if (setId) tagBuilder1.GenerateId(fullHtmlFieldName); ModelState modelState; if (htmlHelper.ViewData.ModelState.TryGetValue(fullHtmlFieldName, out modelState) && modelState.Errors.Count > 0) tagBuilder1.AddCssClass(HtmlHelper.ValidationInputCssClassName); tagBuilder1.MergeAttributes (htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata)); if (inputType != InputType.CheckBox) return new MvcHtmlString(tagBuilder1.ToString(TagRenderMode.SelfClosing)); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(tagBuilder1.ToString(TagRenderMode.SelfClosing)); TagBuilder tagBuilder2 = new TagBuilder("input"); tagBuilder2.MergeAttribute("type", HtmlHelper.GetInputTypeString(InputType.Hidden)); tagBuilder2.MergeAttribute("name", fullHtmlFieldName); tagBuilder2.MergeAttribute("value", "false"); stringBuilder.Append(tagBuilder2.ToString(TagRenderMode.SelfClosing)); return MvcHtmlString.Create(stringBuilder.ToString()); } private static RouteValueDictionary ToRouteValueDictionary(IDictionary dictionary) { if (dictionary != null) return new RouteValueDictionary(dictionary); return new RouteValueDictionary(); } private static object GetModelStateValue(ViewDataDictionary viewData, string key, Type destinationType) { ModelState modelState; if (viewData.ModelState.TryGetValue(key, out modelState)) { if (modelState.Value != null) { return modelState.Value.ConvertTo(destinationType, null /* culture */); } } return null; } private static bool EvalBoolean(ViewDataDictionary viewData, string key) { return Convert.ToBoolean(viewData.Eval(key), CultureInfo.InvariantCulture); } private static string EvalString(ViewDataDictionary viewData, string key) { return Convert.ToString(viewData.Eval(key), CultureInfo.CurrentCulture); } private static string EvalString(ViewDataDictionary viewData, string key, string format) { return Convert.ToString(viewData.Eval(key, format), CultureInfo.CurrentCulture); } }}
ASP.NET MVC 3 扩展之给 HtmlHelper 扩展一个 RadioButtonList
直接贴代码了:
using System;using System.Collections;using System.Collections.Generic;using System.Globalization;using System.Linq;using System.Web.Mvc;using System.Web.Mvc.Html;public static class RadioListExtensions{ public static MvcHtmlString[] RadioButtonList(this HtmlHelper htmlHelper, string name) { return RadioButtonList(htmlHelper, name, (IDictionary)null); } public static MvcHtmlString[] RadioButtonList(this HtmlHelper htmlHelper, string name, object htmlAttributes) { return RadioButtonList(htmlHelper, name, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } public static MvcHtmlString[] RadioButtonList(this HtmlHelper htmlHelper, string name, IDictionary htmlAttributes) { IEnumerable selectList = htmlHelper.GetSelectData(name); return htmlHelper.RadioButtonListInternal(name, selectList, true /* usedViewData */, htmlAttributes); } public static MvcHtmlString[] RadioButtonList(this HtmlHelper htmlHelper, string name, IEnumerable selectList) { return RadioButtonList(htmlHelper, name, selectList, (IDictionary )null); } public static MvcHtmlString[] RadioButtonList(this HtmlHelper htmlHelper, string name, IEnumerable selectList, object htmlAttributes) { return RadioButtonList(htmlHelper, name, selectList, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } public static MvcHtmlString[] RadioButtonList(this HtmlHelper htmlHelper, string name, IEnumerable selectList, IDictionary htmlAttributes) { return htmlHelper.RadioButtonListInternal(name, selectList, false /* usedViewData */, htmlAttributes); } private static IEnumerable GetSelectData(this HtmlHelper htmlHelper, string name) { object o = null; if (htmlHelper.ViewData != null) { o = htmlHelper.ViewData.Eval(name); } if (o == null) { throw new InvalidOperationException( String.Format( CultureInfo.CurrentCulture, "There is no ViewData item with the key '{0}' of type '{1}'.", name, typeof(IEnumerable ))); } IEnumerable selectList = o as IEnumerable ; if (selectList == null) { throw new InvalidOperationException( String.Format( CultureInfo.CurrentCulture, "The ViewData item with the key '{0}' is of type '{1}' but needs to be of type '{2}'.", name, o.GetType().FullName, typeof(IEnumerable ))); } return selectList; } private static MvcHtmlString[] RadioButtonListInternal(this HtmlHelper htmlHelper, string name, IEnumerable selectList, bool usedViewData, IDictionary htmlAttributes) { if (String.IsNullOrEmpty(name)) { throw new ArgumentException("name"); } if (selectList == null) { throw new ArgumentNullException("selectList"); } // If we haven't already used ViewData to get the entire list of items then we need to // use the ViewData-supplied value before using the parameter-supplied value. if (!usedViewData) { object defaultValue = htmlHelper.ViewData.Eval(name); if (defaultValue != null) { IEnumerable defaultValues = new[] { defaultValue }; IEnumerable values = from object value in defaultValues select Convert.ToString(value, CultureInfo.CurrentCulture); HashSet selectedValues = new HashSet (values, StringComparer.OrdinalIgnoreCase); List newSelectList = new List (); foreach (SelectListItem item in selectList) { item.Selected = (item.Value != null) ? selectedValues.Contains(item.Value) : selectedValues.Contains(item.Text); newSelectList.Add(item); } selectList = newSelectList; } } IEnumerable radioButtons = selectList.Select (item => htmlHelper.RadioButton(name, item.Value, item.Selected, htmlAttributes)); return radioButtons.ToArray(); }}
谢谢浏览!