MVC HtmlHelper 生成 CheckBox,CheckBoxList,RadioButtonList
public static class CheckBoxListExtension { #region CheckBoxList public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, Func<dynamic, object> format, object htmlAttributes) { return CheckBoxListFor(html, expression, selectList, format, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, Func<dynamic, object> format = null, IDictionary<string, object> htmlAttributes = null) { return CheckBoxList(html, GetName(expression), selectList, format, htmlAttributes); } public static MvcHtmlString CheckBoxList(this HtmlHelper html, string name, IEnumerable<SelectListItem> selectList, Func<dynamic, object> format, object htmlAttributes) { return CheckBoxList(html, name, selectList, format, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } public static MvcHtmlString CheckBoxList(this HtmlHelper html, string name, IEnumerable<SelectListItem> selectList, Func<dynamic, object> format = null, IDictionary<string, object> htmlAttributes = null) { return InputListInternal(html, name, selectList, true, format, htmlAttributes); } public static MvcHtmlString CTCheckBox(this HtmlHelper html, string name) { return CTCheckBox(html, name, false); } public static MvcHtmlString CTCheckBox(this HtmlHelper html, string name, bool isChecked) { return CTCheckBox(html, name, isChecked, null); } public static MvcHtmlString CTCheckBox(this HtmlHelper html, string name, bool isChecked, object htmlAttributes = null) { return CTCheckBox(html, name, isChecked, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } /// <summary> /// 通過使用指定的 HTML 幫助器、窗體欄位的名稱、用於指示是否已選中核取方塊的值以及 HTML 特性,返回核取方塊 input 元素。 /// </summary> /// <param name="html">此方法擴充套件的 HTML 幫助器例項。</param> /// <param name="name">窗體欄位的名稱。</param> /// <param name="isChecked">如果要選中核取方塊,則為 true;否則為 false。</param> /// <param name="htmlAttributes">一個物件,其中包含要為該元素設定的 HTML 特性。</param> /// <returns></returns> public static MvcHtmlString CTCheckBox(this HtmlHelper html, string name, bool isChecked, IDictionary<string, object> htmlAttributes = null) { string fullHtmlFieldName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name); if (string.IsNullOrEmpty(fullHtmlFieldName)) { throw new ArgumentException("filed can't be null or empty !", "name"); } TagBuilder tagBuilder = new TagBuilder("input"); tagBuilder.MergeAttribute("type", "checkbox", true); if (isChecked) tagBuilder.MergeAttribute("checked", "checked", true); tagBuilder.MergeAttributes<string, object>(htmlAttributes); tagBuilder.MergeAttribute("id", fullHtmlFieldName, true); tagBuilder.MergeAttribute("name", fullHtmlFieldName, true); return new MvcHtmlString(tagBuilder.ToString()); } #endregion #region RadioButtonList public static MvcHtmlString CTRadioButtonList(this HtmlHelper html, string name, IEnumerable<SelectListItem> selectList, Func<dynamic, object> format, object htmlAttributes) { return CTRadioButtonList(html, name, selectList, format, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } public static MvcHtmlString CTRadioButtonList(this HtmlHelper html, string name, IEnumerable<SelectListItem> selectList, Func<dynamic, object> format = null, IDictionary<string, object> htmlAttributes = null) { return InputListInternal(html, name, selectList, false, format, htmlAttributes); } public static MvcHtmlString CTRadioButtonListFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, Func<dynamic, object> format, object htmlAttributes) { return CTRadioButtonList(html, GetName(expression), selectList, format, htmlAttributes); } public static MvcHtmlString CTRadioButtonListFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, Func<dynamic, object> format = null, IDictionary<string, object> htmlAttributes = null) { return CTRadioButtonList(html, GetName(expression), selectList, format, htmlAttributes); } #endregion /*------------------------------------- * Core Function --------------------------------------*/ private static MvcHtmlString InputListInternal(this HtmlHelper html, string name, IEnumerable<SelectListItem> selectList, bool allowMultiple, Func<dynamic, object> format, IDictionary<string, object> htmlAttributes) { string fullHtmlFieldName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name); if (string.IsNullOrEmpty(fullHtmlFieldName)) { throw new ArgumentException("filed can't be null or empty !", "name"); } if (format == null) format = i => i.Button + i.Text + "<br/>"; StringBuilder strBuilder = new StringBuilder(); foreach (var item in selectList) { //Clear first TagBuilder tagBuilder = new TagBuilder("input"); tagBuilder.InnerHtml = string.Empty; if (allowMultiple) { tagBuilder.MergeAttribute("type", "checkbox", true); } else { tagBuilder.MergeAttribute("type", "radio", true); } tagBuilder.MergeAttribute("value", item.Value, true); if (item.Selected) tagBuilder.MergeAttribute("checked", "checked", true); tagBuilder.MergeAttributes<string, object>(htmlAttributes); tagBuilder.MergeAttribute("name", fullHtmlFieldName, true); var btnHtmlString = new MvcHtmlString(tagBuilder.ToString()); var inputItem = new InputListItem { Button = btnHtmlString, Text = item.Text }; var s = format(inputItem).ToString(); strBuilder.Append(s); } return new MvcHtmlString(strBuilder.ToString()); } private static string GetName(LambdaExpression expression) { if (expression == null) { throw new ArgumentNullException("expression"); } return ExpressionHelper.GetExpressionText(expression); } } public class InputListItem { public MvcHtmlString Button { get; set; } public string Text { get; set; } }
內容均為作者獨立觀點,不代表八零IT人立場,如涉及侵權,請及時告知。
相關文章
- MVC-HtmlHelper簡單總結MVCHTML
- checkboxList operate in windows fromWindows
- ASP.Net MVC開發基礎學習筆記(2):HtmlHelper與擴充套件方法ASP.NETMVC筆記HTML套件
- Javascript得到CheckBoxList的ValueJavaScript
- repeater中巢狀放入RadioButtonList巢狀
- 實現checkboxlist的全選
- CheckBoxList擴充套件方法程式碼套件
- checkbox操作
- jQuery :checkboxjQuery
- 解決 Vue 動態生成 el-checkbox 點選無法賦值問題Vue賦值
- 使用js操作checkboxJS
- WPF CheckBox ToolTip Image
- RUF MVC5 Repositories Framework Generator程式碼生成工具介紹和使用MVCFramework
- Checkbox 陣列 不更新陣列
- 自定義 checkbox 新玩法 ?
- 自定義 checkbox 樣式
- GridView與javascript、checkboxViewJavaScript
- van-checkbox + dialog
- asp.net 中RadioButtonList的選項改變事件處理(採用jquery操作)ASP.NET事件jQuery
- MVC5+EF6 入門完整教程13 -- 動態生成多級選單MVC
- Jquery使用[Input type="checkbox&jQuery
- ListView全選刪除CheckBoxView
- CSS3 checkbox美化效果CSSS3
- Android中自定義CheckboxAndroid
- ngGrid checkbox應用
- checkbox樣式研究——按鈕
- 2020.10.22-MVC5過濾器(許可權認證)與checkbox傳值的相關問題MVC過濾器
- MVC 5 + EF6 入門完整教程14 -- 動態生成麵包屑導航MVC
- MVC框架MVC框架
- HTML input checkbox 核取方塊HTML
- checkbox name屬性值注意點
- jQuery操作checkbox選擇程式碼jQuery
- 關於JQuery操作checkbox問題jQuery
- HTML input checkbox核取方塊HTML
- js判斷checkbox是否選中JS
- JavaScript獲取選中checkbox valueJavaScript
- CSS 美化checkbox核取方塊CSS
- js checkbox 全選 取消全選JS