1: public static class InputExtensions
2: {
3: public static string CheckBoxList(this HtmlHelper htmlHelper, string name, List<CheckBoxListInfo> listInfo)
4: {
5: return htmlHelper.CheckBoxList(name, listInfo,
6: ((IDictionary<string, object>) null));
7: }
8:
9: public static string CheckBoxList(this HtmlHelper htmlHelper, string name, List<CheckBoxListInfo> listInfo,
10: object htmlAttributes)
11: {
12: return htmlHelper.CheckBoxList(name, listInfo,
13: ((IDictionary<string, object>)new RouteValueDictionary(htmlAttributes)));
14: }
15:
16: public static string CheckBoxList(this HtmlHelper htmlHelper, string name, List<CheckBoxListInfo> listInfo,
17: IDictionary<string, object> htmlAttributes)
18: {
19: if (String.IsNullOrEmpty(name))
20: throw new ArgumentException("The argument must have a value", "name");
21: if (listInfo == null)
22: throw new ArgumentNullException("listInfo");
23: if (listInfo.Count < 1)
24: throw new ArgumentException("The list must contain at least one value", "listInfo");
25:
26: StringBuilder sb = new StringBuilder();
27:
28: foreach (CheckBoxListInfo info in listInfo)
29: {
30: TagBuilder builder = new TagBuilder("input");
31: if (info.IsChecked) builder.MergeAttribute("checked", "checked");
32: builder.MergeAttributes<string, object>(htmlAttributes);
33: builder.MergeAttribute("type", "checkbox");
34: builder.MergeAttribute("value", info.Value);
35: builder.MergeAttribute("name", name);
36: builder.InnerHtml = info.DisplayText;
37: sb.Append(builder.ToString(TagRenderMode.Normal));
38: sb.Append("<br />");
39: }
40:
41: return sb.ToString();
42: }
43: }
44:
45: // This the information that is needed by each checkbox in the
46: // CheckBoxList helper.
47: public class CheckBoxListInfo
48: {
49: public CheckBoxListInfo(string value, string displayText, bool isChecked)
50: {
51: this.Value = value;
52: this.DisplayText = displayText;
53: this.IsChecked = isChecked;
54: }
55:
56: public string Value { get; private set; }
57: public string DisplayText { get; private set; }
58: public bool IsChecked { get; private set; }
59: }