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