Exemple #1
0
        public static IHtmlString CheckBoxList <T>(this HtmlHelper helper, string name, IEnumerable <T> items, Expression <Func <T, object> > valueExpression, Expression <Func <T, object> > textExpression, CheckBoxListOptions options = null)
        {
            List <CheckBoxListItem> cbliList = new List <CheckBoxListItem>();

            foreach (T item in items)
            {
                CheckBoxListItem cbli = new CheckBoxListItem
                {
                    Value = valueExpression.Compile().Invoke(item).ToString(),
                    Text  = textExpression.Compile().Invoke(item).ToString()
                };
                cbliList.Add(cbli);
            }

            CheckBoxList cbl = new CheckBoxList(cbliList, options);

            return(new HtmlString(cbl.Render(name)));
        }
Exemple #2
0
        public static IHtmlString CheckBoxListFor <TModel, TProperty>(this HtmlHelper <TModel> helper, Expression <Func <TModel, TProperty> > expression, IEnumerable <CheckBoxListItem> items, CheckBoxListOptions options = null)
        {
            string    name = string.Empty;
            TProperty val  = helper.EvalExpression(expression, out name);

            TagBuilder builder     = new TagBuilder("div");
            string     cssClass    = options == null ? string.Empty : options.CssClass;
            string     colCssClass = options == null || string.IsNullOrEmpty(options.ColumnCssClass) ? "col-sm-1" : options.ColumnCssClass;
            int        itemsPerRow = options == null ? 4 : options.ItemsPerRow;

            builder.AddCssClass(("container-fluid checkbox-list " + cssClass).Trim());
            TagBuilder hidden = new TagBuilder("input");

            hidden.Attributes.Add("type", "hidden");
            hidden.Attributes.Add("name", name);
            hidden.Attributes.Add("id", name);
            hidden.Attributes.Add("value", val.ToString());
            builder.InnerHtml = hidden.ToString();
            TagBuilder row = new TagBuilder("div");

            row.AddCssClass("row");
            int index = 0;

            foreach (CheckBoxListItem item in items)
            {
                if (index > 0 && index % itemsPerRow == 0)
                {
                    builder.InnerHtml += row.ToString();
                    row = new TagBuilder("div");
                    row.AddCssClass("row");
                }

                TagBuilder col = new TagBuilder("div");
                col.AddCssClass(colCssClass);

                TagBuilder label = new TagBuilder("label");
                label.AddCssClass("control-label");

                TagBuilder checkbox = new TagBuilder("input");
                checkbox.Attributes.Add("type", "checkbox");
                checkbox.Attributes.Add("value", item.Value);

                label.InnerHtml = string.Format("{0} {1}", checkbox.ToString(), item.Text);

                col.InnerHtml = label.ToString();

                row.InnerHtml += col.ToString();

                index++;
            }

            if (!string.IsNullOrEmpty(row.InnerHtml))
            {
                builder.InnerHtml += row.ToString();
            }

            return(new HtmlString(builder.ToString()));
        }
Exemple #3
0
        public static IHtmlString CheckBoxList(this HtmlHelper helper, string name, IEnumerable <CheckBoxListItem> items = null, CheckBoxListOptions options = null)
        {
            CheckBoxList cbl = new CheckBoxList(items, options);

            return(new HtmlString(cbl.Render(name)));
        }