Click here to Skip to main content
15,891,567 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 771.6K   15K   128  
Extends MVC HtmlHelper class so you can create POSTable checkbox list.
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;

/// <summary>
///  Model-based CheckBoxList extensions
/// </summary>
public static class MvcCheckBoxList_Extensions_Model {
	/// <summary>
	/// Model-Based function (For...)
	/// </summary>
	/// <typeparam name="TModel">Current ViewModel</typeparam>
	/// <typeparam name="TItem">ViewModel Item</typeparam>
	/// <typeparam name="TValue">ViewModel Item type of the value</typeparam>
	/// <typeparam name="TKey">ViewModel Item type of the key</typeparam>
	/// <typeparam name="TProperty">ViewModel property</typeparam>
	/// <param name="htmlHelper">MVC Html helper class that is being extended</param>
	/// <param name="listNameExpr">ViewModel Item type to serve as a name of each checkbox in a list (use this name to POST list values array back to the controller)</param>
	/// <param name="sourceDataExpr">Data list to be used as a source for the list (set in viewmodel)</param>
	/// <param name="valueExpr">Data list value type to be used as checkbox 'Value'</param>
	/// <param name="textToDisplayExpr">Data list value type to be used as checkbox 'Text'</param>
	/// <param name="selectedValuesExpr">Data list of selected items (should be of same data type as a source list)</param>
	/// <param name="htmlAttributesExpr">Data list HTML tag attributes for each checkbox</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxListFor<TModel, TProperty, TItem, TValue, TKey>
		(this HtmlHelper<TModel> htmlHelper,
		 Expression<Func<TModel, TProperty>> listNameExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> sourceDataExpr,
		 Expression<Func<TItem, TValue>> valueExpr,
		 Expression<Func<TItem, TKey>> textToDisplayExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> selectedValuesExpr,
		 Expression<Func<TItem, dynamic>> htmlAttributesExpr = null) {
		var modelMetadata = ModelMetadata.FromLambdaExpression(listNameExpr, htmlHelper.ViewData);
		return MvcCheckBoxList.CheckBoxList_ModelBased
			(htmlHelper, modelMetadata, listNameExpr.toProperty(), sourceDataExpr, valueExpr,
			 textToDisplayExpr, htmlAttributesExpr, selectedValuesExpr, null, null, null);
	}
	/// <summary>
	/// Model-Based function
	/// </summary>
	/// <typeparam name="TModel">Current ViewModel</typeparam>
	/// <typeparam name="TItem">ViewModel Item</typeparam>
	/// <typeparam name="TValue">ViewModel Item type of the value</typeparam>
	/// <typeparam name="TKey">ViewModel Item type of the key</typeparam>
	/// <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="sourceDataExpr">Data list to be used as a source for the list (set in viewmodel)</param>
	/// <param name="valueExpr">Data list value type to be used as checkbox 'Value'</param>
	/// <param name="textToDisplayExpr">Data list value type to be used as checkbox 'Text'</param>
	/// <param name="selectedValuesExpr">Data list of selected items (should be of same data type as a source list)</param>
	/// <param name="htmlAttributesExpr">Data list HTML tag attributes for each checkbox</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxList<TModel, TItem, TValue, TKey>
		(this HtmlHelper<TModel> htmlHelper,
		 string listName,
		 Expression<Func<TModel, IEnumerable<TItem>>> sourceDataExpr,
		 Expression<Func<TItem, TValue>> valueExpr,
		 Expression<Func<TItem, TKey>> textToDisplayExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> selectedValuesExpr,
		 Expression<Func<TItem, dynamic>> htmlAttributesExpr = null) {
		return MvcCheckBoxList.CheckBoxList_ModelBased
			(htmlHelper, null, listName, sourceDataExpr, valueExpr, textToDisplayExpr, htmlAttributesExpr,
			 selectedValuesExpr, null, null, null);
	}

	/// <summary>
	/// Model-Based function (For...)
	/// </summary>
	/// <typeparam name="TModel">Current ViewModel</typeparam>
	/// <typeparam name="TItem">ViewModel Item</typeparam>
	/// <typeparam name="TValue">ViewModel Item type of the value</typeparam>
	/// <typeparam name="TKey">ViewModel Item type of the key</typeparam>
	/// <typeparam name="TProperty">ViewModel property</typeparam>
	/// <param name="htmlHelper">MVC Html helper class that is being extended</param>
	/// <param name="listNameExpr">ViewModel Item type to serve as a name of each checkbox in a list (use this name to POST list values array back to the controller)</param>
	/// <param name="sourceDataExpr">Data list to be used as a source for the list (set in viewmodel)</param>
	/// <param name="valueExpr">Data list value type to be used as checkbox 'Value'</param>
	/// <param name="textToDisplayExpr">Data list value type to be used as checkbox 'Text'</param>
	/// <param name="selectedValuesExpr">Data list of selected items (should be of same data type as a source list)</param>
	/// <param name="position">Direction of the list (e.g. 'Position2.Horizontal' or 'Position2.Vertical')</param>
	/// <param name="htmlAttributesExpr">Data list HTML tag attributes for each checkbox</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxListFor<TModel, TProperty, TItem, TValue, TKey>
		(this HtmlHelper<TModel> htmlHelper,
		 Expression<Func<TModel, TProperty>> listNameExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> sourceDataExpr,
		 Expression<Func<TItem, TValue>> valueExpr,
		 Expression<Func<TItem, TKey>> textToDisplayExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> selectedValuesExpr,
		 Position position,
		 Expression<Func<TItem, dynamic>> htmlAttributesExpr = null) {
		var modelMetadata = ModelMetadata.FromLambdaExpression(listNameExpr, htmlHelper.ViewData);
		return MvcCheckBoxList.CheckBoxList_ModelBased
			(htmlHelper, modelMetadata, listNameExpr.toProperty(), sourceDataExpr, valueExpr,
			 textToDisplayExpr, htmlAttributesExpr, selectedValuesExpr, null, null, null, position);
	}
	/// <summary>
	/// Model-Based function
	/// </summary>
	/// <typeparam name="TModel">Current ViewModel</typeparam>
	/// <typeparam name="TItem">ViewModel Item</typeparam>
	/// <typeparam name="TValue">ViewModel Item type of the value</typeparam>
	/// <typeparam name="TKey">ViewModel Item type of the key</typeparam>
	/// <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="sourceDataExpr">Data list to be used as a source for the list (set in viewmodel)</param>
	/// <param name="valueExpr">Data list value type to be used as checkbox 'Value'</param>
	/// <param name="textToDisplayExpr">Data list value type to be used as checkbox 'Text'</param>
	/// <param name="selectedValuesExpr">Data list of selected items (should be of same data type as a source list)</param>
	/// <param name="position">Direction of the list (e.g. 'Position2.Horizontal' or 'Position2.Vertical')</param>
	/// <param name="htmlAttributesExpr">Data list HTML tag attributes for each checkbox</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxList<TModel, TItem, TValue, TKey>
		(this HtmlHelper<TModel> htmlHelper,
		 string listName,
		 Expression<Func<TModel, IEnumerable<TItem>>> sourceDataExpr,
		 Expression<Func<TItem, TValue>> valueExpr,
		 Expression<Func<TItem, TKey>> textToDisplayExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> selectedValuesExpr,
		 Position position,
		 Expression<Func<TItem, dynamic>> htmlAttributesExpr = null) {
		return MvcCheckBoxList.CheckBoxList_ModelBased
			(htmlHelper, null, listName, sourceDataExpr, valueExpr, textToDisplayExpr, htmlAttributesExpr,
			 selectedValuesExpr, null, null, null, position);
	}

	/// <summary>
	/// Model-Based function (For...)
	/// </summary>
	/// <typeparam name="TModel">Current ViewModel</typeparam>
	/// <typeparam name="TItem">ViewModel Item</typeparam>
	/// <typeparam name="TValue">ViewModel Item type of the value</typeparam>
	/// <typeparam name="TKey">ViewModel Item type of the key</typeparam>
	/// <typeparam name="TProperty">ViewModel property</typeparam>
	/// <param name="htmlHelper">MVC Html helper class that is being extended</param>
	/// <param name="listNameExpr">ViewModel Item type to serve as a name of each checkbox in a list (use this name to POST list values array back to the controller)</param>
	/// <param name="sourceDataExpr">Data list to be used as a source for the list (set in viewmodel)</param>
	/// <param name="valueExpr">Data list value type to be used as checkbox 'Value'</param>
	/// <param name="textToDisplayExpr">Data list value type to be used as checkbox 'Text'</param>
	/// <param name="selectedValuesExpr">Data list of selected items (should be of same data type as a source list)</param>
	/// <param name="htmlAttributes">Each checkbox HTML tag attributes (e.g. 'new { class="somename" }')</param>
	/// <param name="htmlAttributesExpr">Data list HTML tag attributes for each checkbox</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxListFor<TModel, TProperty, TItem, TValue, TKey>
		(this HtmlHelper<TModel> htmlHelper,
		 Expression<Func<TModel, TProperty>> listNameExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> sourceDataExpr,
		 Expression<Func<TItem, TValue>> valueExpr,
		 Expression<Func<TItem, TKey>> textToDisplayExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> selectedValuesExpr,
		 object htmlAttributes,
		 Expression<Func<TItem, dynamic>> htmlAttributesExpr = null) {
		var modelMetadata = ModelMetadata.FromLambdaExpression(listNameExpr, htmlHelper.ViewData);
		return MvcCheckBoxList.CheckBoxList_ModelBased
			(htmlHelper, modelMetadata, listNameExpr.toProperty(), sourceDataExpr, valueExpr,
			 textToDisplayExpr, htmlAttributesExpr, selectedValuesExpr, htmlAttributes, null, null);
	}
	/// <summary>
	/// Model-Based function
	/// </summary>
	/// <typeparam name="TModel">Current ViewModel</typeparam>
	/// <typeparam name="TItem">ViewModel Item</typeparam>
	/// <typeparam name="TValue">ViewModel Item type of the value</typeparam>
	/// <typeparam name="TKey">ViewModel Item type of the key</typeparam>
	/// <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="sourceDataExpr">Data list to be used as a source for the list (set in viewmodel)</param>
	/// <param name="valueExpr">Data list value type to be used as checkbox 'Value'</param>
	/// <param name="textToDisplayExpr">Data list value type to be used as checkbox 'Text'</param>
	/// <param name="selectedValuesExpr">Data list of selected items (should be of same data type as a source list)</param>
	/// <param name="htmlAttributes">Each checkbox HTML tag attributes (e.g. 'new { class="somename" }')</param>
	/// <param name="htmlAttributesExpr">Data list HTML tag attributes for each checkbox</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxList<TModel, TItem, TValue, TKey>
		(this HtmlHelper<TModel> htmlHelper,
		 string listName,
		 Expression<Func<TModel, IEnumerable<TItem>>> sourceDataExpr,
		 Expression<Func<TItem, TValue>> valueExpr,
		 Expression<Func<TItem, TKey>> textToDisplayExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> selectedValuesExpr,
		 object htmlAttributes,
		 Expression<Func<TItem, dynamic>> htmlAttributesExpr = null) {
		return MvcCheckBoxList.CheckBoxList_ModelBased
			(htmlHelper, null, listName, sourceDataExpr, valueExpr, textToDisplayExpr, htmlAttributesExpr,
			 selectedValuesExpr, htmlAttributes, null, null);
	}

	/// <summary>
	/// Model-Based function (For...)
	/// </summary>
	/// <typeparam name="TModel">Current ViewModel</typeparam>
	/// <typeparam name="TItem">ViewModel Item</typeparam>
	/// <typeparam name="TValue">ViewModel Item type of the value</typeparam>
	/// <typeparam name="TKey">ViewModel Item type of the key</typeparam>
	/// <typeparam name="TProperty">ViewModel property</typeparam>
	/// <param name="htmlHelper">MVC Html helper class that is being extended</param>
	/// <param name="listNameExpr">ViewModel Item type to serve as a name of each checkbox in a list (use this name to POST list values array back to the controller)</param>
	/// <param name="sourceDataExpr">Data list to be used as a source for the list (set in viewmodel)</param>
	/// <param name="valueExpr">Data list value type to be used as checkbox 'Value'</param>
	/// <param name="textToDisplayExpr">Data list value type to be used as checkbox 'Text'</param>
	/// <param name="selectedValuesExpr">Data list of selected items (should be of same data type as a source 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>
	/// <param name="htmlAttributesExpr">Data list HTML tag attributes, to allow override of htmlAttributes for each checkbox</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxListFor<TModel, TProperty, TItem, TValue, TKey>
		(this HtmlHelper<TModel> htmlHelper,
		 Expression<Func<TModel, TProperty>> listNameExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> sourceDataExpr,
		 Expression<Func<TItem, TValue>> valueExpr,
		 Expression<Func<TItem, TKey>> textToDisplayExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> selectedValuesExpr,
		 object htmlAttributes,
		 Position position,
		 Expression<Func<TItem, dynamic>> htmlAttributesExpr = null) {
		var modelMetadata = ModelMetadata.FromLambdaExpression(listNameExpr, htmlHelper.ViewData);
		return MvcCheckBoxList.CheckBoxList_ModelBased
			(htmlHelper, modelMetadata, listNameExpr.toProperty(), sourceDataExpr, valueExpr, textToDisplayExpr,
			 htmlAttributesExpr, selectedValuesExpr, htmlAttributes, null, null, position);
	}
	/// <summary>
	/// Model-Based function
	/// </summary>
	/// <typeparam name="TModel">Current ViewModel</typeparam>
	/// <typeparam name="TItem">ViewModel Item</typeparam>
	/// <typeparam name="TValue">ViewModel Item type of the value</typeparam>
	/// <typeparam name="TKey">ViewModel Item type of the key</typeparam>
	/// <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="sourceDataExpr">Data list to be used as a source for the list (set in viewmodel)</param>
	/// <param name="valueExpr">Data list value type to be used as checkbox 'Value'</param>
	/// <param name="textToDisplayExpr">Data list value type to be used as checkbox 'Text'</param>
	/// <param name="selectedValuesExpr">Data list of selected items (should be of same data type as a source 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>
	/// <param name="htmlAttributesExpr">Data list HTML tag attributes, to allow override of htmlAttributes for each checkbox</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxList<TModel, TItem, TValue, TKey>
		(this HtmlHelper<TModel> htmlHelper,
		 string listName,
		 Expression<Func<TModel, IEnumerable<TItem>>> sourceDataExpr,
		 Expression<Func<TItem, TValue>> valueExpr,
		 Expression<Func<TItem, TKey>> textToDisplayExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> selectedValuesExpr,
		 object htmlAttributes,
		 Position position,
		 Expression<Func<TItem, dynamic>> htmlAttributesExpr = null) {
		return MvcCheckBoxList.CheckBoxList_ModelBased
			(htmlHelper, null, listName, sourceDataExpr, valueExpr, textToDisplayExpr, htmlAttributesExpr,
			 selectedValuesExpr, htmlAttributes, null, null, position);
	}

	/// <summary>
	/// Model-Based function (For...)
	/// </summary>
	/// <typeparam name="TModel">Current ViewModel</typeparam>
	/// <typeparam name="TItem">ViewModel Item</typeparam>
	/// <typeparam name="TValue">ViewModel Item type of the value</typeparam>
	/// <typeparam name="TKey">ViewModel Item type of the key</typeparam>
	/// <typeparam name="TProperty">ViewModel property</typeparam>
	/// <param name="htmlHelper">MVC Html helper class that is being extended</param>
	/// <param name="listNameExpr">ViewModel Item type to serve as a name of each checkbox in a list (use this name to POST list values array back to the controller)</param>
	/// <param name="sourceDataExpr">Data list to be used as a source for the list (set in viewmodel)</param>
	/// <param name="valueExpr">Data list value type to be used as checkbox 'Value'</param>
	/// <param name="textToDisplayExpr">Data list value type to be used as checkbox 'Text'</param>
	/// <param name="selectedValuesExpr">Data list of selected items (should be of same data type as a source 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>
	/// <param name="htmlAttributesExpr">Data list HTML tag attributes, to allow override of htmlAttributes for each checkbox</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxListFor<TModel, TProperty, TItem, TValue, TKey>
		(this HtmlHelper<TModel> htmlHelper,
		 Expression<Func<TModel, TProperty>> listNameExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> sourceDataExpr,
		 Expression<Func<TItem, TValue>> valueExpr,
		 Expression<Func<TItem, TKey>> textToDisplayExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> selectedValuesExpr,
		 object htmlAttributes,
		 string[] disabledValues,
		 Position position,
		 Expression<Func<TItem, dynamic>> htmlAttributesExpr = null) {
		var modelMetadata = ModelMetadata.FromLambdaExpression(listNameExpr, htmlHelper.ViewData);
		return MvcCheckBoxList.CheckBoxList_ModelBased
			(htmlHelper, modelMetadata, listNameExpr.toProperty(), sourceDataExpr, valueExpr, textToDisplayExpr,
			 htmlAttributesExpr, selectedValuesExpr, htmlAttributes, null, disabledValues, position);
	}
	/// <summary>
	/// Model-Based function
	/// </summary>
	/// <typeparam name="TModel">Current ViewModel</typeparam>
	/// <typeparam name="TItem">ViewModel Item</typeparam>
	/// <typeparam name="TValue">ViewModel Item type of the value</typeparam>
	/// <typeparam name="TKey">ViewModel Item type of the key</typeparam>
	/// <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="sourceDataExpr">Data list to be used as a source for the list (set in viewmodel)</param>
	/// <param name="valueExpr">Data list value type to be used as checkbox 'Value'</param>
	/// <param name="textToDisplayExpr">Data list value type to be used as checkbox 'Text'</param>
	/// <param name="selectedValuesExpr">Data list of selected items (should be of same data type as a source 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>
	/// <param name="htmlAttributesExpr">Data list HTML tag attributes, to allow override of htmlAttributes for each checkbox</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxList<TModel, TItem, TValue, TKey>
		(this HtmlHelper<TModel> htmlHelper,
		 string listName,
		 Expression<Func<TModel, IEnumerable<TItem>>> sourceDataExpr,
		 Expression<Func<TItem, TValue>> valueExpr,
		 Expression<Func<TItem, TKey>> textToDisplayExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> selectedValuesExpr,
		 object htmlAttributes,
		 string[] disabledValues,
		 Position position,
		 Expression<Func<TItem, dynamic>> htmlAttributesExpr = null) {
		return MvcCheckBoxList.CheckBoxList_ModelBased
			(htmlHelper, null, listName, sourceDataExpr, valueExpr, textToDisplayExpr, htmlAttributesExpr,
			 selectedValuesExpr, htmlAttributes, null, disabledValues, position);
	}

	/// <summary>
	/// Model-Based function (For...)
	/// </summary>
	/// <typeparam name="TModel">Current ViewModel</typeparam>
	/// <typeparam name="TItem">ViewModel Item</typeparam>
	/// <typeparam name="TValue">ViewModel Item type of the value</typeparam>
	/// <typeparam name="TKey">ViewModel Item type of the key</typeparam>
	/// <typeparam name="TProperty">ViewModel property</typeparam>
	/// <param name="htmlHelper">MVC Html helper class that is being extended</param>
	/// <param name="listNameExpr">ViewModel Item type to serve as a name of each checkbox in a list (use this name to POST list values array back to the controller)</param>
	/// <param name="sourceDataExpr">Data list to be used as a source for the list (set in viewmodel)</param>
	/// <param name="valueExpr">Data list value type to be used as checkbox 'Value'</param>
	/// <param name="textToDisplayExpr">Data list value type to be used as checkbox 'Text'</param>
	/// <param name="selectedValuesExpr">Data list of selected items (should be of same data type as a source 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="htmlAttributesExpr">Data list HTML tag attributes for each checkbox</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxListFor<TModel, TProperty, TItem, TValue, TKey>
		(this HtmlHelper<TModel> htmlHelper,
		 Expression<Func<TModel, TProperty>> listNameExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> sourceDataExpr,
		 Expression<Func<TItem, TValue>> valueExpr,
		 Expression<Func<TItem, TKey>> textToDisplayExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> selectedValuesExpr,
		 HtmlListInfo wrapInfo,
		 Expression<Func<TItem, dynamic>> htmlAttributesExpr = null) {
		var modelMetadata = ModelMetadata.FromLambdaExpression(listNameExpr, htmlHelper.ViewData);
		return MvcCheckBoxList.CheckBoxList_ModelBased
			(htmlHelper, modelMetadata, listNameExpr.toProperty(), sourceDataExpr, valueExpr, textToDisplayExpr,
			 htmlAttributesExpr, selectedValuesExpr, null, wrapInfo, null);
	}
	/// <summary>
	/// Model-Based function
	/// </summary>
	/// <typeparam name="TModel">Current ViewModel</typeparam>
	/// <typeparam name="TItem">ViewModel Item</typeparam>
	/// <typeparam name="TValue">ViewModel Item type of the value</typeparam>
	/// <typeparam name="TKey">ViewModel Item type of the key</typeparam>
	/// <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="sourceDataExpr">Data list to be used as a source for the list (set in viewmodel)</param>
	/// <param name="valueExpr">Data list value type to be used as checkbox 'Value'</param>
	/// <param name="textToDisplayExpr">Data list value type to be used as checkbox 'Text'</param>
	/// <param name="selectedValuesExpr">Data list of selected items (should be of same data type as a source 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="htmlAttributesExpr">Data list HTML tag attributes for each checkbox</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxList<TModel, TItem, TValue, TKey>
		(this HtmlHelper<TModel> htmlHelper,
		 string listName,
		 Expression<Func<TModel, IEnumerable<TItem>>> sourceDataExpr,
		 Expression<Func<TItem, TValue>> valueExpr,
		 Expression<Func<TItem, TKey>> textToDisplayExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> selectedValuesExpr,
		 HtmlListInfo wrapInfo,
		 Expression<Func<TItem, dynamic>> htmlAttributesExpr = null) {
		return MvcCheckBoxList.CheckBoxList_ModelBased
			(htmlHelper, null, listName, sourceDataExpr, valueExpr, textToDisplayExpr, htmlAttributesExpr,
			 selectedValuesExpr, null, wrapInfo, null);
	}

	/// <summary>
	/// Model-Based function (For...)
	/// </summary>
	/// <typeparam name="TModel">Current ViewModel</typeparam>
	/// <typeparam name="TItem">ViewModel Item</typeparam>
	/// <typeparam name="TValue">ViewModel Item type of the value</typeparam>
	/// <typeparam name="TKey">ViewModel Item type of the key</typeparam>
	/// <typeparam name="TProperty">ViewModel property</typeparam>
	/// <param name="htmlHelper">MVC Html helper class that is being extended</param>
	/// <param name="listNameExpr">ViewModel Item type to serve as a name of each checkbox in a list (use this name to POST list values array back to the controller)</param>
	/// <param name="sourceDataExpr">Data list to be used as a source for the list (set in viewmodel)</param>
	/// <param name="valueExpr">Data list value type to be used as checkbox 'Value'</param>
	/// <param name="textToDisplayExpr">Data list value type to be used as checkbox 'Text'</param>
	/// <param name="selectedValuesExpr">Data list of selected items (should be of same data type as a source 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>
	/// <param name="htmlAttributesExpr">Data list HTML tag attributes for each checkbox</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxListFor<TModel, TProperty, TItem, TValue, TKey>
		(this HtmlHelper<TModel> htmlHelper,
		 Expression<Func<TModel, TProperty>> listNameExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> sourceDataExpr,
		 Expression<Func<TItem, TValue>> valueExpr,
		 Expression<Func<TItem, TKey>> textToDisplayExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> selectedValuesExpr,
		 HtmlListInfo wrapInfo,
		 string[] disabledValues,
		 Expression<Func<TItem, dynamic>> htmlAttributesExpr = null) {
		var modelMetadata = ModelMetadata.FromLambdaExpression(listNameExpr, htmlHelper.ViewData);
		return MvcCheckBoxList.CheckBoxList_ModelBased
			(htmlHelper, modelMetadata, listNameExpr.toProperty(), sourceDataExpr, valueExpr, textToDisplayExpr,
			 htmlAttributesExpr, selectedValuesExpr, null, wrapInfo, disabledValues);
	}
	/// <summary>
	/// Model-Based function
	/// </summary>
	/// <typeparam name="TModel">Current ViewModel</typeparam>
	/// <typeparam name="TItem">ViewModel Item</typeparam>
	/// <typeparam name="TValue">ViewModel Item type of the value</typeparam>
	/// <typeparam name="TKey">ViewModel Item type of the key</typeparam>
	/// <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="sourceDataExpr">Data list to be used as a source for the list (set in viewmodel)</param>
	/// <param name="valueExpr">Data list value type to be used as checkbox 'Value'</param>
	/// <param name="textToDisplayExpr">Data list value type to be used as checkbox 'Text'</param>
	/// <param name="selectedValuesExpr">Data list of selected items (should be of same data type as a source 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>
	/// <param name="htmlAttributesExpr">Data list HTML tag attributes for each checkbox</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxList<TModel, TItem, TValue, TKey>
		(this HtmlHelper<TModel> htmlHelper,
		 string listName,
		 Expression<Func<TModel, IEnumerable<TItem>>> sourceDataExpr,
		 Expression<Func<TItem, TValue>> valueExpr,
		 Expression<Func<TItem, TKey>> textToDisplayExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> selectedValuesExpr,
		 HtmlListInfo wrapInfo,
		 string[] disabledValues,
		 Expression<Func<TItem, dynamic>> htmlAttributesExpr = null) {
		return MvcCheckBoxList.CheckBoxList_ModelBased
			(htmlHelper, null, listName, sourceDataExpr, valueExpr, textToDisplayExpr, htmlAttributesExpr,
			 selectedValuesExpr, null, wrapInfo, disabledValues);
	}

	/// <summary>
	/// Model-Based function (For...)
	/// </summary>
	/// <typeparam name="TModel">Current ViewModel</typeparam>
	/// <typeparam name="TItem">ViewModel Item</typeparam>
	/// <typeparam name="TValue">ViewModel Item type of the value</typeparam>
	/// <typeparam name="TKey">ViewModel Item type of the key</typeparam>
	/// <typeparam name="TProperty">ViewModel property</typeparam>
	/// <param name="htmlHelper">MVC Html helper class that is being extended</param>
	/// <param name="listNameExpr">ViewModel Item type to serve as a name of each checkbox in a list (use this name to POST list values array back to the controller)</param>
	/// <param name="sourceDataExpr">Data list to be used as a source for the list (set in viewmodel)</param>
	/// <param name="valueExpr">Data list value type to be used as checkbox 'Value'</param>
	/// <param name="textToDisplayExpr">Data list value type to be used as checkbox 'Text'</param>
	/// <param name="selectedValuesExpr">Data list of selected items (should be of same data type as a source 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="htmlAttributesExpr">Data list HTML tag attributes, to allow override of htmlAttributes for each checkbox</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxListFor<TModel, TProperty, TItem, TValue, TKey>
		(this HtmlHelper<TModel> htmlHelper,
		 Expression<Func<TModel, TProperty>> listNameExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> sourceDataExpr,
		 Expression<Func<TItem, TValue>> valueExpr,
		 Expression<Func<TItem, TKey>> textToDisplayExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> selectedValuesExpr,
		 object htmlAttributes,
		 HtmlListInfo wrapInfo,
		 string[] disabledValues,
		 Expression<Func<TItem, dynamic>> htmlAttributesExpr = null) {
		var modelMetadata = ModelMetadata.FromLambdaExpression(listNameExpr, htmlHelper.ViewData);
		return MvcCheckBoxList.CheckBoxList_ModelBased
			(htmlHelper, modelMetadata, listNameExpr.toProperty(), sourceDataExpr, valueExpr, textToDisplayExpr,
			 htmlAttributesExpr, selectedValuesExpr, htmlAttributes, wrapInfo, disabledValues);
	}
	/// <summary>
	/// Model-Based function
	/// </summary>
	/// <typeparam name="TModel">Current ViewModel</typeparam>
	/// <typeparam name="TItem">ViewModel Item</typeparam>
	/// <typeparam name="TValue">ViewModel Item type of the value</typeparam>
	/// <typeparam name="TKey">ViewModel Item type of the key</typeparam>
	/// <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="sourceDataExpr">Data list to be used as a source for the list (set in viewmodel)</param>
	/// <param name="valueExpr">Data list value type to be used as checkbox 'Value'</param>
	/// <param name="textToDisplayExpr">Data list value type to be used as checkbox 'Text'</param>
	/// <param name="selectedValuesExpr">Data list of selected items (should be of same data type as a source 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="htmlAttributesExpr">Data list HTML tag attributes, to allow override of htmlAttributes for each checkbox</param>
	/// <returns>HTML string containing checkbox list</returns>
	public static MvcHtmlString CheckBoxList<TModel, TItem, TValue, TKey>
		(this HtmlHelper<TModel> htmlHelper,
		 string listName,
		 Expression<Func<TModel, IEnumerable<TItem>>> sourceDataExpr,
		 Expression<Func<TItem, TValue>> valueExpr,
		 Expression<Func<TItem, TKey>> textToDisplayExpr,
		 Expression<Func<TModel, IEnumerable<TItem>>> selectedValuesExpr,
		 object htmlAttributes,
		 HtmlListInfo wrapInfo,
		 string[] disabledValues,
		 Expression<Func<TItem, dynamic>> htmlAttributesExpr = null) {
		return MvcCheckBoxList.CheckBoxList_ModelBased
			(htmlHelper, null, listName, sourceDataExpr, valueExpr, textToDisplayExpr, htmlAttributesExpr,
			 selectedValuesExpr, htmlAttributes, wrapInfo, disabledValues);
	}
}

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