/// <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> /// Generates Model-based list of checkboxes (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="selectedValueExpr">Boolean value from db or selector corresponding to each item to be selected</param> /// <param name="htmlAttributes">Applies custom HTML tag attributes to each checkbox/label combo</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">Applies custom HTML tag attributes to each checkbox/label combo if defined locally (e.g.: entity => new { tagName = "tagValue" }), or to particular combos, if defined in the database (e.g.: entity => entity.TagsDbObject)</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 <TItem, bool> > selectedValueExpr, object htmlAttributes, HtmlListInfo wrapInfo, string[] disabledValues, Expression <Func <TItem, object> > htmlAttributesExpr) { var modelMetadata = ModelMetadata.FromLambdaExpression(listNameExpr, htmlHelper.ViewData); return(_listBuilder.CheckBoxList (new listConstructor <TModel, TItem, TValue, TKey> { htmlHelper = htmlHelper, modelMetadata = modelMetadata, listName = listNameExpr.toProperty(), sourceDataExpr = sourceDataExpr, valueExpr = valueExpr, textToDisplayExpr = textToDisplayExpr, htmlAttributesExpr = htmlAttributesExpr, selectedValueExpr = selectedValueExpr, htmlAttributes = htmlAttributes, htmlListInfo = wrapInfo, disabledValues = disabledValues })); }
/// <summary> /// Generates Model-based list of checkboxes (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> /// <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="selectedValueExpr">Boolean value from db or selector corresponding to each item to be selected</param> /// <param name="htmlAttributes">Applies custom HTML tag attributes to each checkbox/label combo</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">Applies custom HTML tag attributes to each checkbox/label combo if defined locally (e.g.: entity => new { tagName = "tagValue" }), or to particular combos, if defined in the database (e.g.: entity => entity.TagsDbObject)</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 <TItem, bool> > selectedValueExpr, object htmlAttributes, HtmlListInfo wrapInfo, string[] disabledValues, Expression <Func <TItem, object> > htmlAttributesExpr) { return(_listBuilder.CheckBoxList (new listConstructor <TModel, TItem, TValue, TKey> { htmlHelper = htmlHelper, listName = listName, sourceDataExpr = sourceDataExpr, valueExpr = valueExpr, textToDisplayExpr = textToDisplayExpr, htmlAttributesExpr = htmlAttributesExpr, selectedValueExpr = selectedValueExpr, htmlAttributes = htmlAttributes, htmlListInfo = wrapInfo, disabledValues = 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); }
/// <summary> /// Model-Based main 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="modelMetadata">Model Metadata</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="htmlAttributesExpr">Data list HTML tag attributes, to allow override of htmlAttributes for each checkbox (e.g. 'item => new { data_relation_id = item.RelationID }')</param> /// <param name="selectedValuesExpr">Data list of selected items (should be of same data type as a source list)</param> /// <param name="selectedValueExpr">Boolean value from db or selector corresponding to each item to be selected</param> /// <param name="htmlAttributes">Each checkbox HTML tag attributes (e.g. 'new { class="somename" }')</param> /// <param name="htmlListInfo">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> private MvcHtmlString _listBuilder <TModel, TItem, TValue, TKey> (HtmlHelper <TModel> htmlHelper, ModelMetadata modelMetadata, string listName, Expression <Func <TModel, IEnumerable <TItem> > > sourceDataExpr, Expression <Func <TItem, TValue> > valueExpr, Expression <Func <TItem, TKey> > textToDisplayExpr, Expression <Func <TItem, object> > htmlAttributesExpr, Expression <Func <TModel, IEnumerable <TItem> > > selectedValuesExpr, Expression <Func <TItem, bool> > selectedValueExpr, object htmlAttributes, HtmlListInfo htmlListInfo, string[] disabledValues, Position position = Position.Horizontal) { // ---------------------------------------------------------------------- // initial validation // ---------------------------------------------------------------------- if (sourceDataExpr == null || sourceDataExpr.Body.ToString() == "null") { return(MvcHtmlString.Create(no_data_message)); } if (htmlHelper.ViewData.Model == null) { throw new NoNullAllowedException(empty_model_message); } if (string.IsNullOrEmpty(listName)) { throw new ArgumentException(empty_name_message, "listName"); } // ---------------------------------------------------------------------- // ---------------------------------------------------------------------- // get data from the model // ---------------------------------------------------------------------- // model var model = htmlHelper.ViewData.Model; // source data list var sourceData = sourceDataExpr.Compile()(model).ToList(); // function to get value of the output list element var _valueFunc = valueExpr.Compile(); // function to get label (text) of the output list element var _textToDisplayFunc = textToDisplayExpr.Compile(); // get a list of selected items var selectedItems = new List <TItem>(); if (selectedValuesExpr != null) { var selectedItems_temp = selectedValuesExpr.Compile()(model); if (selectedItems_temp != null) { selectedItems = selectedItems_temp.ToList(); } } var selectedValues = selectedItems.Select(s => _valueFunc(s).ToString()).ToList(); // ---------------------------------------------------------------------- // validate source data if (!sourceData.Any()) { return(MvcHtmlString.Create(no_data_message)); } // if HtmlListInfo is provided, then check for inverse text direction var textLayout = TextLayout.Default; if (htmlListInfo != null && htmlListInfo.TextLayout == TextLayout.RightToLeft) { textLayout = htmlListInfo.TextLayout; } if (position == Position.Vertical_RightToLeft || position == Position.Horizontal_RightToLeft) { textLayout = TextLayout.RightToLeft; } // set up table/list html wrapper, if applicable var numberOfItems = sourceData.Count; var htmlWrapper = _createHtmlWrapper(htmlListInfo, numberOfItems, position, textLayout); // create checkbox list var sb = new StringBuilder(); sb.Append(htmlWrapper.wrap_open); htmlwrap_rowbreak_counter = 0; // create list of checkboxes based on data foreach (var item in sourceData) { // get checkbox value, text, and selectio from expressionFunction var itemValue = _valueFunc(item).ToString(); string itemText; if (htmlListInfo != null && htmlListInfo.TemplateIsUsed == TemplateIsUsed.Yes) { // Use Shared\DisplayTemplates\City.cshtml to render template // (has to have same name as Class for which it templates) // City.cshtml example: // ----------------------------------------- //@model MvcCheckBoxListSampleApp.Model.City //<strong>@Model.Name</strong> //- test display template // ----------------------------------------- itemText = htmlHelper.DisplayFor(x => item).ToString(); } else { itemText = _textToDisplayFunc(item).ToString(); } // function to get a selected value from boolean variable var itemIsSelected = ""; if (selectedValueExpr != null) { var _selectedValueFunc = selectedValueExpr.Compile(); itemIsSelected = _selectedValueFunc(item).ToString(); } // get a dictionary of html attributes var htmlAttributesForCheckBox = item.getHtmlAttributes(htmlAttributes, htmlAttributesExpr); // create checkbox element sb = _createCheckBoxListElement (sb, htmlHelper, modelMetadata, htmlWrapper, htmlAttributesForCheckBox, selectedValues, itemIsSelected, disabledValues, listName, itemValue, itemText, textLayout); } sb.Append(htmlWrapper.wrap_close); // return checkbox list return(MvcHtmlString.Create(sb.ToString())); }
/// <summary> /// Creates an HTML wrapper for the checkbox list /// </summary> /// <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="numberOfItems">Count of all items in the list</param> /// <param name="position">Direction of the list (e.g. 'Position2.Horizontal' or 'Position2.Vertical')</param> /// <param name="textLayout">Sets layout of a checkbox for right-to-left languages</param> /// <returns>HTML wrapper information</returns> private htmlWrapperInfo _createHtmlWrapper (HtmlListInfo wrapInfo, int numberOfItems, Position position, TextLayout textLayout) { var w = new htmlWrapperInfo(); if (wrapInfo != null) { // creating custom layouts switch (wrapInfo.htmlTag) { // creates user selected number of float sections with // vertically sorted checkboxes case HtmlTag.vertical_columns: { if (wrapInfo.Columns <= 0) { wrapInfo.Columns = 1; } // calculate number of rows var rows = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(numberOfItems) / Convert.ToDecimal(wrapInfo.Columns))); if (numberOfItems <= 4 && (numberOfItems <= wrapInfo.Columns || numberOfItems - wrapInfo.Columns == 1)) { rows = numberOfItems; } w.separator_max_counter = rows; // create wrapped raw html tag var wrapRow = htmlElementTag.div; var wrapHtml_builder = new TagBuilder(wrapRow.ToString()); var user_html_attributes = wrapInfo.htmlAttributes.toDictionary(); // create base columnt style and merge it with user provided style (if applicable) var defaultSectionStyle = "float:left;"; if (textLayout == TextLayout.RightToLeft) { defaultSectionStyle += " text-align: right;"; } object style; user_html_attributes.TryGetValue("style", out style); if (style != null) // if user style is set, use it { wrapHtml_builder.MergeAttribute("style", defaultSectionStyle + " " + style); } else // if not set, add only default style { wrapHtml_builder.MergeAttribute("style", defaultSectionStyle); } // merge it with other user provided attributes (e.g.: class) user_html_attributes.Remove("style"); wrapHtml_builder.MergeAttributes(user_html_attributes); // build wrapped raw html tag w.wrap_open = wrapHtml_builder.ToString(TagRenderMode.StartTag); w.wrap_rowbreak = "</" + wrapRow + "> " + wrapHtml_builder.ToString(TagRenderMode.StartTag); w.wrap_close = wrapHtml_builder.ToString(TagRenderMode.EndTag) + " <div style=\"clear:both;\"></div>"; w.append_to_element = "<br/>"; } break; // creates an html <table> with checkboxes sorted horizontally case HtmlTag.table: { if (wrapInfo.Columns <= 0) { wrapInfo.Columns = 1; } w.separator_max_counter = wrapInfo.Columns; var wrapHtml_builder = new TagBuilder(htmlElementTag.table.ToString()); wrapHtml_builder.MergeAttributes(wrapInfo.htmlAttributes.toDictionary()); wrapHtml_builder.MergeAttribute("cellspacing", "0"); // for IE7 compatibility var wrapRow = htmlElementTag.tr; w.wrap_element = htmlElementTag.td; w.wrap_open = wrapHtml_builder.ToString(TagRenderMode.StartTag) + "<" + wrapRow + ">"; w.wrap_rowbreak = "</" + wrapRow + "><" + wrapRow + ">"; w.wrap_close = "</" + wrapRow + ">" + wrapHtml_builder.ToString(TagRenderMode.EndTag); } break; } } // default setting creates vertical or horizontal column of checkboxes else { if (position == Position.Horizontal || position == Position.Horizontal_RightToLeft) { w.append_to_element = " "; } if (position == Position.Vertical || position == Position.Vertical_RightToLeft) { w.append_to_element = "<br/>"; } if (textLayout == TextLayout.RightToLeft) { // lean text to right for right-to-left languages var defaultSectionStyle = "style=\"text-align: right;\""; var wrapRow = htmlElementTag.div; w.wrap_open = "<" + wrapRow + " " + defaultSectionStyle + ">"; w.wrap_rowbreak = string.Empty; w.wrap_close = "</" + wrapRow + ">"; } } // return completed check box list wrapper return(w); }
public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string listName, List<SelectListItem> dataList, HtmlListInfo wrapInfo) { return htmlHelper.CheckBoxList(listName, dataList, null, wrapInfo, null, Position.Horizontal); }