一步一步封装自己的HtmlHelper组件BootstrapHelper(二(4)

将上面的命名空间注释掉之后,在cshtml页面里面我们将不能再使用Html变量的相关扩展方法,如果你自己的Helper够用,不用原生的扩展方法问题应该也不大。

三、RadioExtensions和CheckboxExtensions

关于bootstrap里面的radio和checkbox组件,博主参考了下里面的写法,进行了如下封装:

RadioExtensions.cs

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace BootstrapExtensions { public static class RadioExtensions { /// <summary> /// 返回表单radio标签 /// </summary> /// <param>扩展方法实例</param> /// <param>name属性</param> /// <returns>返回radio标签</returns> public static MvcHtmlString Radio(this BootstrapHelper html, string name) { return Radio(html, null, name, null, null, null, false, false, null); } /// <summary> /// 返回表单radio标签 /// </summary> /// <param>扩展方法实例</param> /// <param>id</param> /// <param>name属性</param> /// <returns>返回radio标签</returns> public static MvcHtmlString Radio(this BootstrapHelper html, string id, string name) { return Radio(html, id, name, null, null, null, false, false, null); } /// <summary> /// 返回表单radio标签 /// </summary> /// <param>扩展方法实例</param> /// <param>id</param> /// <param>name属性</param> /// <param>是否选中</param> /// <returns>返回radio标签</returns> public static MvcHtmlString Radio(this BootstrapHelper html, string id, string name, bool isCheck) { return Radio(html, id, name, null, null, null, isCheck, false, null); } /// <summary> /// 返回表单radio标签 /// </summary> /// <param>扩展方法实例</param> /// <param>id</param> /// <param>name属性</param> /// <param>input的value值</param> /// <returns>返回radio标签</returns> public static MvcHtmlString Radio(this BootstrapHelper html, string id, string name, object value) { return Radio(html, id, name, value, null, null, false, false, null); } /// <summary> /// 返回表单radio标签 /// </summary> /// <param>扩展方法实例</param> /// <param>id</param> /// <param>name属性</param> /// <param>input的value值</param> /// <param>显示文本</param> /// <returns>返回radio标签</returns> public static MvcHtmlString Radio(this BootstrapHelper html, string id, string name, object value, string text) { return Radio(html, id, name, value, text, null, false, false, null); } /// <summary> /// 返回表单radio标签 /// </summary> /// <param>扩展方法实例</param> /// <param>id</param> /// <param>name属性</param> /// <param>input的value值</param> /// <param>显示文本</param> /// <param>是否选中</param> /// <returns>返回radio标签</returns> public static MvcHtmlString Radio(this BootstrapHelper html, string id, string name, object value, string text, bool isCheck) { return Radio(html, id, name, value, text, null, isCheck, false, null); } /// <summary> /// 返回表单radio标签 /// </summary> /// <param>扩展方法实例</param> /// <param>id</param> /// <param>name属性</param> /// <param>input的value值</param> /// <param>显示文本</param> /// <param>label标签的样式</param> /// <returns>返回radio标签</returns> public static MvcHtmlString Radio(this BootstrapHelper html, string id, string name, object value, string text, string labelClass) { return Radio(html, id, name, value, text, labelClass, false, false, null); } /// <summary> /// 返回表单radio标签 /// </summary> /// <param>扩展方法实例</param> /// <param>id</param> /// <param>name属性</param> /// <param>input的value值</param> /// <param>显示文本</param> /// <param>label标签的样式</param> /// <param>是否选中</param> /// <returns>返回radio标签</returns> public static MvcHtmlString Radio(this BootstrapHelper html, string id, string name, object value, string text, string labelClass, bool isCheck) { return Radio(html, id, name, value, text, labelClass, isCheck, false, null); } /// <summary> /// 返回表单radio标签 /// </summary> /// <param>扩展方法实例</param> /// <param>id</param> /// <param>name属性</param> /// <param>input的value值</param> /// <param>显示文本</param> /// <param>label标签的样式</param> /// <param>是否选中</param> /// <param>是否禁用</param> /// <returns>返回radio标签</returns> public static MvcHtmlString Radio(this BootstrapHelper html, string id, string name, object value, string text, string labelClass, bool isCheck, bool isDisabled) { return Radio(html, id, name, value, text, labelClass, isCheck, isDisabled, null); } /// <summary> /// 返回表单radio标签 /// </summary> /// <param>扩展方法实例</param> /// <param>id</param> /// <param>name属性</param> /// <param>input的value值</param> /// <param>显示文本</param> /// <param>label标签的样式</param> /// <param>是否选中</param> /// <param>是否禁用</param> /// <param>额外标签</param> /// <returns>返回radio标签</returns> public static MvcHtmlString Radio(this BootstrapHelper html, string id, string name, object value, string text, string labelClass, bool isCheck, bool isDisabled, object oAttributes) { IDictionary<string, object> htmlAttributes = null; if (oAttributes != null) { htmlAttributes = BootstrapHelper.AnonymousObjectToHtmlAttributes(oAttributes); } else { htmlAttributes = new Dictionary<string, object>(); } return InputExtensions.CheckBox(html, InputType.Radio, id, name, value, text, labelClass, isCheck, isDisabled, htmlAttributes); } } }

CheckboxExtensions.cs

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzjssj.html