Click here to Skip to main content
15,896,154 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.3K   15K   128  
Extends MVC HtmlHelper class so you can create POSTable checkbox list.
@model dynamic

@{
  ViewBag.Title = "Documentation";
}

<div class="col_12">
  <h3>@ViewBag.Title</h3>
  
  @Html.Partial("GivenPartial")

  <section>
    <title>Base overloads</title>
  
    <p>
      <h6>Base checkbox list call structure:</h6>
<pre style="width: 45%;" class="content_left">
&#64;Html.CheckBoxList("LIST_NAME", 
                   model => model.LIST_DATA, 
                   entity => entity.VALUE, 
                   entity => entity.NAME, 
                   model => model.SELECTED_VALUES)
                   // or entity => entity.IS_CHECKED
</pre>
      <div class="margin_left17 margin_top40 content_left"><h6>OR</h6></div>
<pre style="width: 45%;" class="content_right">
&#64;Html.CheckBoxListFor(model => model.LIST_NAME, 
                      model => model.LIST_DATA, 
                      entity => entity.VALUE, 
                      entity => entity.NAME, 
                      model => model.SELECTED_VALUES)
                      // or entity => entity.IS_CHECKED
</pre>
      <div class="clear"></div>
    </p>
  
    <p>
      <h6>And in our example it looks like this:</h6>
<pre>
&#64;Html.CheckBoxListFor(model => model.PostedCities.CityIDs, 
                      model => model.AvailableCities, 
                      entity => entity.Id, 
                      entity => entity.Name, 
                      model => model.SelectedCities)
</pre>
    </p>
    
    <p>
      <h6>Or if using boolean selector:</h6>
<pre>
&#64;Html.CheckBoxListFor(model => model.PostedCities.CityIDs, 
                      model => model.AvailableCities, 
                      entity => entity.Id, 
                      entity => entity.Name, 
                      entity => entity.IsSelected)
</pre>
      where <code>entity.IsSelected</code> is a boolean value from database
    </p>
    
    <p>
      <h6>And another way to use boolean selector:</h6>
      If we have a list of selected values on our View -
      <code>var selectedIds = new List&lt;int&gt;  { 1, 3, 5 };</code>
      it'll look like this:
<pre>
&#64;Html.CheckBoxListFor(model => model.PostedCities.CityIDs, 
                      model => model.AvailableCities, 
                      entity => entity.Id, 
                      entity => entity.Name, 
                      entity => selectedIds.Contains(entity.Id))
</pre>
      where <code>selectedIds.Contains(entity.Id)</code> returns bool if item Id matches the list
    </p>

  </section>

  <section>
    <title>Basic Settings</title>

    <p>
      Since five base properties don't change, only extra ones will be shown, so placeholder
      <code>...</code> means usage of five base properties
    </p>
    
    <p>
      You might need to add this reference to your view first (namespace for <code>Position</code> <strong>enum</strong> and others):
      <pre>&#64;using MvcCheckBoxList.Model</pre>
    </p>

    <p>
      <h6>1. Set position (direction) of the list:</h6>
      <pre>&#64;Html.CheckBoxListFor( ... , Position.Horizontal)</pre>
    </p>
    <p>
      <code>Position</code> Can be <code>Position.Horizontal</code>, <code>Position.Vertical</code>,
      <code>Position.Horizontal_RightToLeft</code>, or <code>Position.Vertical_RightToLeft</code>
      where last two are to reverse checkbox and label for right-to-left languages
    </p>
  
    <p>
      <h6>2. Set html attributes for both checkbox and label:</h6>
      <pre>&#64;Html.CheckBoxListFor( ... , x => new { &#64;class="class_name" }) // Tags will be applied to all checkbox/label combos</pre>
      or get Tags object from database:
      <pre>&#64;Html.CheckBoxListFor( ... , x => x.Tags) // x.Tags will be applied only to particular checkbox/label combo</pre>
    </p>
  
    <p>
      <h6>3. Set html attributes and position:</h6>
      <pre>&#64;Html.CheckBoxListFor( ... , Position.Horizontal, x => new { &#64;class="class_name" })</pre>
    </p>
  
    <p>
      <h6>4. Set html attributes for all, disabled values, position, and individual html attributes (all attributes will be merged together):</h6>
      <pre>&#64;Html.CheckBoxListFor( ... , x => new { &#64;class="class_name" }, new[] {"3", "5", "7"}, Position.Horizontal, x => x.Tags)</pre>
    </p>

  </section>

  <section>  
    <title>Advanced Settings</title>
  
    <p>
      You might need to add this reference to your view first (namespace for <code>HtmlListInfo</code> <strong>class</strong> and others):
      <pre>&#64;using MvcCheckBoxList.Model</pre>
    </p>    

    <p>
      <h6>1. Set custom layout using <code>HtmlListInfo</code> class:</h6>
<pre>
var htmlListInfo = new HtmlListInfo(HtmlTag.table, 2, new { &#64;class="class_name" }, TextLayout.Default, TemplateIsUsed.Yes);

&#64;Html.CheckBoxListFor( ... , htmlListInfo)
</pre>
    </p>
    <p>
      There, in <code>HtmlListInfo</code> class, <code>HtmlTag</code> can be
      <code>HtmlTag.table</code> or <code>HtmlTag.vertical_columns</code>;
      <code>2</code> is a number of columns;
    </p>
    <p>
      <code>TextLayout</code> can be <code>TextLayout.Default</code>
      or <code>TextLayout.RightToLeft</code> (for right to left languages)
    </p>
  
    <p>
      <h6>2. Set layout with <code>HtmlListInfo</code> class and set html attributes:</h6>
<pre>
&#64;Html.CheckBoxListFor( ... , htmlListInfo, x => new { tagName = "tagValue" })  // Tags will be applied to all checkbox/label combos
</pre>
      or get Tags object from database:
<pre>
&#64;Html.CheckBoxListFor( ... , htmlListInfo, x => x.Tags })  // x.Tags will be applied only to particular checkbox/label combo
</pre>
    </p>
  
    <p>
      <h6>
        3. Set html attributes for all, set layout with <code>HtmlListInfo</code>,
        set disabled values, and individual html attributes (all attributes will be merged together):
      </h6>
<pre>
&#64;Html.CheckBoxListFor( ... , new { &#64;class="class_name" }, htmlListInfo, new[] {"3", "5", "7"}, x => x.Tags)
</pre>
      There, <code>x.Tags</code> is a value of type <code>object</code> and should be
      equal to something similar to this <code>new { tag1 = "value1", tag2="value2" }</code>
      and represent one or more custom html attributes, and will be applied to both
      checkbox and label
    </p>
  
    <h6>
      Also note that <code>x.Tags</code> is an optional parameter for <strong>each</strong>
      available overload. Just add it as a last parameter to 
      <code>&#64;Html.CheckBoxListFor( ... , x => x.Tags)</code>
      checkbox list call
    </h6>

  </section>


  <h4 class="col_12 center">For more information and discussion see</h4>

  <div class="col_12 center">
    <a href="@ViewBag.CodePrejectUrl" class="button large pop">
      <span class="icon" data-icon="?"></span>
      Extension's Article on CodeProject.com
    </a>
  </div>
</div>

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