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<string, object> 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<string, object>(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 = null, string 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
/// 复选框扩展。
/// </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<string, object> 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<string, object>(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 = null, string 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