zoukankan      html  css  js  c++  java
  • MVC3 CheckBoxList RadioButtonList 扩展


     View Code

     直接复制即可使用。 
    /// <summary>
             
    /// 复选框扩展。
             
    /// </summary>
             
    /// <typeparam name="TModel">模型类型。</typeparam>
             
    /// <typeparam name="TProperty">属性类型。</typeparam>
             
    /// <param name="helper">HTML辅助方法。</param>
             
    /// <param name="expression">lambda表达式。</param>
             
    /// <param name="selectList">选择项。</param>
             
    /// <param name="htmlAttributes">HTML属性。</param>
             
    /// <returns>返回复选框MVC的字符串。</returns>
             public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, ButtonFormatter formatter = ButtonFormatter.Horizontal, IDictionary<stringobject> htmlAttributes = null)
             {
                 if(selectList == null || expression == null)
                     return MvcHtmlString.Empty;
                 string name = ExpressionHelper.GetExpressionText(expression);
                 List<string> values = null;
                 object obj = helper.ViewData.Eval(name);
                 if(obj != null)
                     values = obj.ToString().Split<string>();
                 else
                     values = new List<string>();
     
                 StringBuilder sb = new StringBuilder();
                 int index = 0;
                 foreach(var item in selectList)
                 {
                     TagBuilder tag = new TagBuilder("input");
                     tag.MergeAttributes<stringobject>(htmlAttributes);
                     tag.MergeAttribute("type""checkbox"true);
                     tag.MergeAttribute("name", name, true);
                     tag.MergeAttribute("id", name + index, true);
                     tag.MergeAttribute("value", item.Value, true);
                     if(values.Contains(item.Value))
                         tag.MergeAttribute("checked""checked"true);
                     sb.AppendLine(tag.ToString(TagRenderMode.SelfClosing) + " ");
                     TagBuilder label = new TagBuilder("label");
                     label.MergeAttribute("for", name + index);
                     label.InnerHtml = item.Text;
                     sb.AppendLine(label.ToString());
                     if(formatter == ButtonFormatter.Vertical)
                         sb.AppendLine("<br />");
                     index++;
                 }
                 return new MvcHtmlString(sb.ToString());
             }
     
             /// <summary>
             
    /// 复选框扩展。
             
    /// </summary>
             
    /// <typeparam name="TModel">模型类型。</typeparam>
             
    /// <typeparam name="TProperty">属性类型。</typeparam>
             
    /// <param name="helper">HTML辅助方法。</param>
             
    /// <param name="expression">lambda表达式。</param>
             
    /// <param name="selectList">选择项。</param>
             
    /// <param name="htmlAttributes">HTML属性。</param>
             
    /// <returns>返回复选框MVC的字符串。</returns>
             public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, ButtonFormatter formatter, object htmlAttributes)
             {
                 return helper.CheckBoxListFor<TModel, TProperty>(expression, selectList, formatter, new RouteValueDictionary(htmlAttributes));
             }
     
             /// <summary>
             
    /// 枚举复选框扩展。
             
    /// </summary>
             
    /// <typeparam name="TModel">模型类型。</typeparam>
             
    /// <typeparam name="TProperty">属性类型。</typeparam>
             
    /// <param name="helper">HTML辅助方法。</param>
             
    /// <param name="expression">lambda表达式。</param>
             
    /// <param name="selectList">选择项。</param>
             
    /// <param name="htmlAttributes">HTML属性。</param>
             
    /// <returns>返回复选框MVC的字符串。</returns>
             public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, ButtonFormatter formatter = ButtonFormatter.Horizontal, object htmlAttributes = nullstring classKey = Globals.Resource)
             {
                 var selectList = new List<SelectListItem>();
                 Globals.ForEach<TProperty>(p => {
                     selectList.Add(new SelectListItem { Text = Globals.GetGlobalResourceByKey(p, classKey), Value = p.ToString() });
                 });
                 return
  • 相关阅读:
    .Net使用分布式缓存 C# 使用Redis
    微信申请退款API~~开发
    微信支付和支付宝支付分账接口文档
    Android xUtils3.0使用手册(一)- 基础功能使用
    支付宝支付开发——当面付条码支付和扫码支付
    vue开源项目汇总
    Azure和插件发布
    SqlServer数据库优化笔记
    企业微信通过PostMan获取accesstoken与管理员信息方法
    VisualStudio插件自动加载
  • 原文地址:https://www.cnblogs.com/lpe110/p/2799686.html
Copyright © 2011-2022 走看看