MVC3 CheckBoxList RadioButtonList 扩展
2012-12-03 15:09
232 查看
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>
/// 复选框扩展。
/// </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
相关文章推荐
- 为MVC3扩展CheckBoxList和RadioButtonList
- 为MVC3扩展CheckBoxList和RadioButtonList
- 为MVC3扩展CheckBoxList和RadioButtonList
- 扩展RadioButtonListFor和CheckBoxListFor
- Asp.net js获取CheckBoxlist 和 Radiobuttonlist
- ASP.NET 2.0中的友好CSS控件适配器的应用 - 用ul、li呈现CheckBoxList和RadioButtonList
- ASP.NET 2.0中的友好CSS控件适配器的应用 - 用ul、li呈现CheckBoxList和RadioButtonList
- ASP.NET 2.0中的友好CSS控件适配器的应用 - 用ul、li呈现CheckBoxList和RadioButtonList
- Jquery获取CheckBoxList的值(兼容火狐,附js方法)RadioButtonList同样适用
- Silverlight中利用ListBox特性实现单选按钮组RadioButtonList和复选按钮组CheckBoxList的功能
- CheckBoxList和RadioButtonList控件的绑定
- JQuery设置与获取RadioButtonList和CheckBoxList的值
- asp.net中checkboxlist和radiobuttonlist
- RadioButtonList 和CheckBoxList根据后天数据库的值加载时选中其选项
- .NET MVC标签扩展(checkbox,radio)
- 对HtmlHelper扩展二:添加RadioButtonList、CheckBoxList
- MVC扩展生成CheckBoxList并水平排列
- ASP.NET MVC 3 扩展之给 HtmlHelper 扩展一个 RadioButtonList
- 扩展CheckBoxList实现选中绑定
- 扩展CheckBoxList实现选中绑定