Click here to Skip to main content
15,896,201 members
Articles / Web Development / HTML

CheckBoxList(For) - A missing MVC extension (no longer supported)

Rate me:
Please Sign up or sign in to vote.
4.82/5 (44 votes)
4 Feb 2014CPOL6 min read 777.4K   15K   128  
Extends MVC HtmlHelper class so you can create POSTable checkbox list.
using System.Collections.Generic;
using System.Web.Mvc;

/// <summary>
/// Regular CheckBoxList extensions
/// </summary>
public static class MvcCheckBoxList_Extensions_Basic {
	/// <summary>
	/// Model-Independent function
	/// </summary>
	/// <param name="htmlHelper">MVC Html helper class that is being extended</param>
	/// <param name="listName">Name of each checkbox in a list (use this name to POST list values array back to the controller)</param>
	/// <param name="dataList">List of name/value pairs to be used as source data for the list</param>
	/// <param name="position">Direction of the list (e.g. 'Position2.Horizontal' or 'Position2.Vertical')</param>
	/// <returns>HTML string containing checkbox list</returns>	
	public static MvcHtmlString CheckBoxList
		(this HtmlHelper htmlHelper, string listName, List<SelectListItem> dataList,
		 Position position = Position.Horizontal) {
		return htmlHelper.CheckBoxList
			(listName, dataList, null, null, null, position);
	}
	/// <summary>
	/// Model-Independent function
	/// </summary>
	/// <param name="htmlHelper">MVC Html helper class that is being extended</param>
	/// <param name="listName">Name of each checkbox in a list (use this name to POST list values array back to the controller)</param>
	/// <param name="dataList">List of name/value pairs to be used as source data for the list</param>
	/// <param name="htmlAttributes">Each checkbox HTML tag attributes (e.g. 'new { class="somename" }')</param>
	/// <param name="position">Direction of the list (e.g. 'Position2.Horizontal' or 'Position2.Vertical')</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxList
		(this HtmlHelper htmlHelper, string listName, List<SelectListItem> dataList,
		 object htmlAttributes, Position position = Position.Horizontal) {
		return htmlHelper.CheckBoxList
			(listName, dataList, htmlAttributes, null, null, position);
	}
	/// <summary>
	/// Model-Independent function
	/// </summary>
	/// <param name="htmlHelper">MVC Html helper class that is being extended</param>
	/// <param name="listName">Name of each checkbox in a list (use this name to POST list values array back to the controller)</param>
	/// <param name="dataList">List of name/value pairs to be used as source data for the list</param>
	/// <param name="htmlAttributes">Each checkbox HTML tag attributes (e.g. 'new { class="somename" }')</param>
	/// <param name="disabledValues">String array of values to disable</param>
	/// <param name="position">Direction of the list (e.g. 'Position2.Horizontal' or 'Position2.Vertical')</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxList
		(this HtmlHelper htmlHelper, string listName, List<SelectListItem> dataList,
		 object htmlAttributes, string[] disabledValues, Position position = Position.Horizontal) {
		return htmlHelper.CheckBoxList
			(listName, dataList, htmlAttributes, null, disabledValues, position);
	}
	/// <summary>
	/// Model-Independent function
	/// </summary>
	/// <param name="htmlHelper">MVC Html helper class that is being extended</param>
	/// <param name="listName">Name of each checkbox in a list (use this name to POST list values array back to the controller)</param>
	/// <param name="dataList">List of name/value pairs to be used as source data for the list</param>
	/// <param name="wrapInfo">Settings for HTML wrapper of the list (e.g. 'new HtmlListInfo2(HtmlTag2.vertical_columns, 2, new { style="color:green;" })')</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxList
		(this HtmlHelper htmlHelper, string listName, List<SelectListItem> dataList,
		 HtmlListInfo wrapInfo) {
		return htmlHelper.CheckBoxList
			(listName, dataList, null, wrapInfo, null);
	}
	/// <summary>
	/// Model-Independent function
	/// </summary>
	/// <param name="htmlHelper">MVC Html helper class that is being extended</param>
	/// <param name="listName">Name of each checkbox in a list (use this name to POST list values array back to the controller)</param>
	/// <param name="dataList">List of name/value pairs to be used as source data for the list</param>
	/// <param name="wrapInfo">Settings for HTML wrapper of the list (e.g. 'new HtmlListInfo2(HtmlTag2.vertical_columns, 2, new { style="color:green;" })')</param>
	/// <param name="disabledValues">String array of values to disable</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxList
		(this HtmlHelper htmlHelper, string listName, List<SelectListItem> dataList,
		 HtmlListInfo wrapInfo, string[] disabledValues) {
		return htmlHelper.CheckBoxList
			(listName, dataList, null, wrapInfo, disabledValues);
	}
	/// <summary>
	/// Model-Independent function
	/// </summary>
	/// <param name="htmlHelper">MVC Html helper class that is being extended</param>
	/// <param name="listName">Name of each checkbox in a list (use this name to POST list values array back to the controller)</param>
	/// <param name="dataList">List of name/value pairs to be used as source data for the list</param>
	/// <param name="htmlAttributes">Each checkbox HTML tag attributes (e.g. 'new { class="somename" }')</param>
	/// <param name="wrapInfo">Settings for HTML wrapper of the list (e.g. 'new HtmlListInfo2(HtmlTag2.vertical_columns, 2, new { style="color:green;" })')</param>
	/// <param name="disabledValues">String array of values to disable</param>
	/// <param name="position">Direction of the list (e.g. 'Position2.Horizontal' or 'Position2.Vertical')</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxList
		(this HtmlHelper htmlHelper, string listName, List<SelectListItem> dataList,
		 object htmlAttributes, HtmlListInfo wrapInfo, string[] disabledValues,
		 Position position = Position.Horizontal) {
		return MvcCheckBoxList.CheckBoxList
			(htmlHelper, listName, dataList, htmlAttributes, wrapInfo, disabledValues, position);
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Coding is awesome!

Comments and Discussions