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.
using System.Collections.Generic;
using System.ComponentModel;

namespace MvcCheckBoxList.Library {
  internal static class ToDictionary_Helper {
    /// <summary>
    /// Convert object to Dictionary of strings and objects
    /// </summary>
    /// <param name="_object">Object of Dictionary of strings and objects (e.g. 'new { name="value" }')</param>
    /// <returns>Dictionary of strings and objects</returns>
    internal static IDictionary<string, object> toDictionary(this object _object) {
      if (_object == null) return new Dictionary<string, object>();
      if (_object is IDictionary<string, object>) return (IDictionary<string, object>) _object;
      var object_properties = TypeDescriptor.GetProperties(_object);
      var dictionary = new Dictionary<string, object>(object_properties.Count);
      foreach (PropertyDescriptor property in object_properties) {
        var name = property.Name.Replace("_", "-");
        // JRR - Added the Replace call. This is the standard used by MVC http://www.asp.net/whitepapers/mvc3-release-notes#0.1__Toc274034227
        var value = property.GetValue(_object);
        dictionary.Add(name, value ?? "");
      }
      return dictionary;
    }
  }
}

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