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人立場,如涉及侵權,請及時告知。
相關文章
- checkboxList operate in windows fromWindows
- repeater中巢狀放入RadioButtonList巢狀
- checkbox操作
- 2020.10.22-MVC5過濾器(許可權認證)與checkbox傳值的相關問題MVC過濾器
- 解決 Vue 動態生成 el-checkbox 點選無法賦值問題Vue賦值
- 使用js操作checkboxJS
- WPF CheckBox ToolTip Image
- van-checkbox + dialog
- Yii2 中 checkboxlist 核取方塊 預設選中
- checkbox與radio美化效果
- Checkbox 陣列 不更新陣列
- CSS3 checkbox美化效果CSSS3
- checkbox/radio自定義樣式
- Jquery使用[Input type="checkbox&jQuery
- jQuery的radio,checkbox,select操作jQuery
- jQuery Validate checkbox和radio驗證jQuery
- 關於JQuery操作checkbox問題jQuery
- aspx中的checkbox 取值問題
- HTML input checkbox核取方塊HTML
- AndroidJava動態修改CheckBox樣式AndroidJava
- CSS 美化checkbox核取方塊CSS
- JavaScript獲取選中checkbox valueJavaScript
- js checkbox 全選 取消全選JS
- js判斷checkbox是否選中JS
- HTML input checkbox 核取方塊HTML
- checkbox name屬性值注意點
- Flutter控制元件-- CheckBox 和 CheckboxListTileFlutter控制元件
- jQuery操作checkbox選擇程式碼jQuery
- (!ori)MVCMVC
- MVC框架MVC框架
- spring - mvcSpringMVC
- CSS 搞事技巧:checkbox+label+selectorCSS
- WPF CheckBox控制元件 我全都要控制元件
- 如何擴大CheckBox點選區域
- The prefix “mvc“ for element “mvc:default-servlet-handler“ is not bound.MVCServlet
- MVC字串處理及MVC@RenderSection小計MVC字串
- checkbox 核取方塊全選程式碼
- vue checkbox 實現全選,取消全選Vue