mvc html.checkbox,MVC HtmlHelper 生成 CheckBox,CheckBoxList,RadioButtonList

MVC HtmlHelper 生成 CheckBox,CheckBoxList,RadioButtonList

时间:6年前

浏览:636

[站内原创,转载请注明出处]

public static class CheckBoxListExtension

{

#region CheckBoxList

public static MvcHtmlString CheckBoxListFor(this HtmlHelper html, Expression> expression, IEnumerable selectList, Func format, object htmlAttributes)

{

return CheckBoxListFor(html, expression, selectList, format, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));

}

public static MvcHtmlString CheckBoxListFor(this HtmlHelper html, Expression> expression, IEnumerable selectList, Func format = null, IDictionary htmlAttributes = null)

{

return CheckBoxList(html, GetName(expression), selectList, format, htmlAttributes);

}

public static MvcHtmlString CheckBoxList(this HtmlHelper html, string name, IEnumerable selectList, Func format, object htmlAttributes)

{

return CheckBoxList(html, name, selectList, format, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));

}

public static MvcHtmlString CheckBoxList(this HtmlHelper html, string name, IEnumerable selectList, Func format = null, IDictionary 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));

}

///

/// 通过使用指定的 HTML 帮助器、窗体字段的名称、用于指示是否已选中复选框的值以及 HTML 特性,返回复选框 input 元素。

///

/// 此方法扩展的 HTML 帮助器实例。

/// 窗体字段的名称。

/// 如果要选中复选框,则为 true;否则为 false。

/// 一个对象,其中包含要为该元素设置的 HTML 特性。

///

public static MvcHtmlString CTCheckBox(this HtmlHelper html, string name, bool isChecked, IDictionary 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(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 selectList, Func format, object htmlAttributes)

{

return CTRadioButtonList(html, name, selectList, format, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));

}

public static MvcHtmlString CTRadioButtonList(this HtmlHelper html, string name, IEnumerable selectList, Func format = null, IDictionary htmlAttributes = null)

{

return InputListInternal(html, name, selectList, false, format, htmlAttributes);

}

public static MvcHtmlString CTRadioButtonListFor(this HtmlHelper html, Expression> expression, IEnumerable selectList, Func format, object htmlAttributes)

{

return CTRadioButtonList(html, GetName(expression), selectList, format, htmlAttributes);

}

public static MvcHtmlString CTRadioButtonListFor(this HtmlHelper html, Expression> expression, IEnumerable selectList, Func format = null, IDictionary htmlAttributes = null)

{

return CTRadioButtonList(html, GetName(expression), selectList, format, htmlAttributes);

}

#endregion

/*-------------------------------------

* Core Function

--------------------------------------*/

private static MvcHtmlString InputListInternal(this HtmlHelper html, string name, IEnumerable selectList, bool allowMultiple, Func format, IDictionary 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 + "
";

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(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人立场,如涉及侵权,请及时告知。

本站文章除注明转载外,均为本站原创,欢迎任何形式的转载,但请务必注明出处,尊重他人劳动共创和谐网络社区。

转载请注明:文章转载自-八零IT人 [http://www.80iter.com]

本文标题:MVC HtmlHelper 生成 CheckBox,CheckBoxList,RadioButtonList

本文地址:http://www.80iter.com/blog/1416993550579370

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值