Exemple #1
0
        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, IList <CheckBoxListItem> items,
                                                 CheckBoxListSettings settings, object htmlAttributes)
        {
            var stringBuilder = new StringBuilder();

            TagBuilder HtmlBody = GenerateHtmlMarkupOuterTag(settings.cblLayout,
                                                             new RouteValueDictionary(htmlAttributes));

            int iMod            = items.Count % (int)settings.cblRepeatColumns;
            int iterationsCount = items.Count / (int)settings.cblRepeatColumns + (iMod == 0 ? 0 : 1);

            for (int i = 0; i < iterationsCount; i++)
            {
                stringBuilder.Append(GenerateHtmlBeginRow(settings.cblLayout));
                foreach (CheckBoxListItem item in items.Where((item, index) =>
                                                              settings.cblDirection == Direction.Horizontal
                                                                  ? index / (int)settings.cblRepeatColumns == i
                                                                  : (index - i) % iterationsCount == 0))
                {
                    stringBuilder.AppendFormat("{0}{1}{2}{3} ",
                                               GenerateHtmlMarkupCheckBox(item, settings),
                                               GenerateHtmlMiddleRow(settings.cblLayout),
                                               GenerateHtmlMarkupLabel(item, string.Format("{0}_{1}", settings.cblName, item.Id)),
                                               GenerateHtmlMiddleRow(settings.cblLayout));
                }
                stringBuilder.Append(GenerateHtmlEndRow(settings.cblLayout));
            }

            HtmlBody.InnerHtml = stringBuilder.ToString();
            return(new MvcHtmlString(HtmlBody.ToString(TagRenderMode.Normal)));
        }
Exemple #2
0
        public static string GenerateHtmlMarkupCheckBox(CheckBoxListItem item, CheckBoxListSettings settings)
        {
            var tagBuilder = new TagBuilder("input");

            tagBuilder.MergeAttribute("id", string.Format("{0}_{1}", settings.cblName, item.Id));
            tagBuilder.MergeAttribute("type", "checkbox");
            tagBuilder.MergeAttribute("name", settings.cblName);
            tagBuilder.MergeAttribute("value", item.Id.ToString());
            if (item.Check)
            {
                tagBuilder.MergeAttribute("checked", "checked");
            }

            if (settings.Disabled)
            {
                tagBuilder.MergeAttribute("disabled", "disabled");
            }

            return(tagBuilder.ToString(TagRenderMode.SelfClosing));
        }