中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

愛上(shang)MVC~為(wei)CheckBoxFor和RadioButtonFor加個擴展方(fang)法吧(希望MVC5把這方(fang)法收納——呵(he)呵(he))

回到目錄

說在前

我都是(shi)喜歡把問題復雜化,還(huan)有總是(shi)喜歡把問題簡單(dan)化,偷懶化,這也需就是(shi)一個程序員的追求吧,呵呵。

我(wo)(wo)不(bu)太喜歡重(zhong)復(fu)的(de)(de)(de)(de)(de)東西,當你看到頁面上有一個(ge)以上相同的(de)(de)(de)(de)(de)代碼(ma)(ma)(ma)時,那可以說(shuo)(shuo),你的(de)(de)(de)(de)(de)代碼(ma)(ma)(ma)有重(zhong)構(gou)的(de)(de)(de)(de)(de)余地,應該(gai)考慮重(zhong)構(gou)了(le),今天看了(le)以前同事(shi)的(de)(de)(de)(de)(de)博客,寫了(le)關于DropDownList的(de)(de)(de)(de)(de)用法(fa)(fa),如(ru)何將集(ji)合數據(ju)綁定到下拉列表框(kuang)上,講的(de)(de)(de)(de)(de)不(bu)錯,但最后(hou)在說(shuo)(shuo)checkbox和radiobutton時,我(wo)(wo)感覺有點(dian)重(zhong)復(fu)了(le),我(wo)(wo)說(shuo)(shuo)的(de)(de)(de)(de)(de)是代碼(ma)(ma)(ma)重(zhong)復(fu)了(le),還有就(jiu)(jiu)是代碼(ma)(ma)(ma)復(fu)雜(za)化(hua)了(le),就(jiu)(jiu)是說(shuo)(shuo),我(wo)(wo)再使用集(ji)合來產(chan)生(sheng)checkbox和radiobutton時,代碼(ma)(ma)(ma)有些復(fu)雜(za)了(le),這不(bu)是我(wo)(wo)們希(xi)望看到的(de)(de)(de)(de)(de),所(suo)以,我(wo)(wo)覺得(de)有必要(yao)把它重(zhong)構(gou)一下,為mvc加(jia)兩(liang)個(ge)擴展方(fang)法(fa)(fa)吧,呵呵。

做在后

DropDownListFor的方法簽名做的不(bu)錯,很面向對象,很簡潔,但不(bu)知(zhi)不(bu)何,checkbox和radiobutton確沒有這樣的重(zhong)載,呵呵

        public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList);
        public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, IDictionary<string, object> htmlAttributes);
        public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, object htmlAttributes);
        public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel);
        public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel, IDictionary<string, object> htmlAttributes);
        public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel, object htmlAttributes);

對于checkbox和radiobutton它們只(zhi)有簡單的(de)幾個重載,沒有對集合(he)的(de)支(zhi)持,所(suo)以,我的(de)這次(ci)代碼擴展,主要是針(zhen)對集合(he)來說的(de),看(kan)方法簽名:

下面看一(yi)(yi)下原代碼,它直接寫在(zai)了System.Web.Mvc命名(ming)空間下,這更說明我是對微軟MVC的一(yi)(yi)個擴展,希望(wang)MVC5出來(lai)時,可以把這些都(dou)集成進去(qu),呵(he)呵(he)

#region 單選框和復選框的擴展
        /// <summary>
        /// 復選(xuan)框,selValue為選(xuan)中項
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name"></param>
        /// <param name="selectList"></param>
        /// <param name="selValue"></param>
        /// <returns></returns>
        public static MvcHtmlString CheckBox(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, IEnumerable<string> selValue)
        {
            return CheckBoxAndRadioFor<object, string>(name, selectList, false, selValue);
        }
        public static MvcHtmlString CheckBox(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, string selValue)
        {
            return CheckBox(htmlHelper, name, selectList, new List<string> { selValue });

        }
        /// <summary>
        /// 復(fu)選框
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name"></param>
        /// <param name="selectList"></param>
        /// <returns></returns>
        public static MvcHtmlString CheckBoxFor(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList)
        {
            return CheckBox(htmlHelper, name, selectList, new List<string>());
        }
        /// <summary>
        /// 根據(ju)列表輸出checkbox
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="selectList"></param>
        /// <returns></returns>
        public static MvcHtmlString CheckBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList)
        {
            return CheckBoxFor(htmlHelper, expression, selectList, null);
        }

        /// <summary>
        ///  根據列表輸(shu)出checkbox,selValue為(wei)默認選中的項
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="selectList"></param>
        /// <param name="selValue"></param>
        /// <returns></returns>
        public static MvcHtmlString CheckBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string selValue)
        {
            string name = ExpressionHelper.GetExpressionText(expression);
            return CheckBoxAndRadioFor<TModel, TProperty>(name, selectList, false, new List<string> { selValue });
        }
        /// <summary>
        /// 輸出單選(xuan)框和復選(xuan)框
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <;typeparam name="TProperty"></typeparam>
        /// <param name="expression"></param>
        /// <param name="selectList"></param>
        /// <param name="isRadio"></param>
        /// <param name="selValue"></param>
        /// <returns></returns>
        static MvcHtmlString CheckBoxAndRadioFor<TModel, TProperty>(
            string name,
            IEnumerable<SelectListItem> selectList,
            bool isRadio,
            IEnumerable<string> selValue)
        {
            StringBuilder str = new StringBuilder();
            int c = 0;
            string check, activeClass;
            string type = isRadio ? "Radio" : "checkbox";

            foreach (var item in selectList)
            {
                c++;
                if (selValue != null && selValue.Contains(item.Value))
                {
                    check = "checked='checked'";
                    activeClass = "style=color:red";
                }
                else
                {
                    check = string.Empty;
                    activeClass = string.Empty;
                }
                str.AppendFormat("<span><input type='{3}' value='{0}' name={1} id={1}{2} " + check + "/>", item.Value, name, c, type);
                str.AppendFormat("&lt;label for='{0}{1}' {3}>{2}</lable></span>", name, c, item.Text, activeClass);

            }
            return MvcHtmlString.Create(str.ToString());
        }


        public static MvcHtmlString RadioButton(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, IEnumerable<string> selValue)
        {
            return CheckBoxAndRadioFor<object, string>(name, selectList, true, selValue);
        }
        /// <summary>
        /// 單選按(an)鈕組,seletList為選中(zhong)項
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name"></param>
        /// <param name="selectList"></param>
        /// <param name="selValue"></param>
        /// <returns></returns>
        public static MvcHtmlString RadioButton(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, string selValue)
        {
            return RadioButton(htmlHelper, name, selectList, new List<string> { selValue });
        }
        /// <summary>
        /// 單選按(an)鈕組
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name"></param>
        /// <param name="selectList"></param>
        /// <returns></returns>
        public static MvcHtmlString RadioButton(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList)
        {
            return RadioButton(htmlHelper, name, selectList, new List<string>());
        }
        /// <summary>
        ///  根(gen)據(ju)列表輸出radiobutton
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="selectList"></param>
        /// <returns></returns>
        public static MvcHtmlString RadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList)
        {
            return RadioButtonFor(htmlHelper, expression, selectList, new List<string>());
        }
        public static MvcHtmlString RadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, IEnumerable<string> selValue)
        {
            string name = ExpressionHelper.GetExpressionText(expression);
            return CheckBoxAndRadioFor<TModel, TProperty>(name, selectList, true, selValue);
        }
        /// <summary>
        /// 根據(ju)列表輸出radiobutton,selValue為(wei)默認選中(zhong)的項
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="selectList"></param>
        /// <param name="selValue"></param>
        /// <returns></returns>
        public static MvcHtmlString RadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string selValue)
        {
            return RadioButtonFor(htmlHelper, expression, selectList, new List<string> { selValue });
        }
        #endregion

回到目錄

posted @ 2013-12-17 22:33  張占嶺  閱讀(4951)  評論(2)    收藏  舉報