Click here to Skip to main content
15,895,840 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 777K   15K   128  
Extends MVC HtmlHelper class so you can create POSTable checkbox list.
  @* Given *@
  <p class="margin_top30">
    <h4>Given we have...</h4>
  </p>
  <p class="margin_top30">
    <h5>Base class <code>City</code></h5>
<pre>
public class City {
  public int Id { get; set; }           // Integer value of a checkbox
  public string Name { get; set; }      // String name of a checkbox
  public object Tags { get; set; }      // Object of html tags to be applied to checkbox, e.g.: 'new { tagName = "tagValue" }'
  public bool IsSelected { get; set; }  // Boolean value to select a checkbox on the list
}
</pre>
    <h5>And we use <code>CitiesViewModel</code> view model on our view</h5>
<pre>
public class CitiesViewModel {
  public IList&lt;City&gt; AvailableCities { get; set; }
  public IList&lt;City&gt; SelectedCities { get; set; }
  public PostedCities PostedCities { get; set; }
}

// Helper class to make posting back selected values easier
public class PostedCities {
  public string[] CityIDs { get; set; }
}
</pre>
    <h5>And our controller accepts class <code>PostedCities</code></h5>
<pre>
public ActionResult Examples(PostedCities postedCities) {
  return View(/* Create View Model */);
}
</pre>
  </p>
  @* end Given *@

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