MVC HtmlHelper 生成 CheckBox,CheckBoxList,RadioButtonList

CnctSoft發表於2019-05-11

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人立場,如涉及侵權,請及時告知。

相關文章