Example #1
0
 public static MvcHtmlString CheckBoxListFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, IEnumerable <SelectListItem> selectList, string optionLabel, object htmlAttributes, ColNumber cols)
 {
     return(CheckBoxListFor(htmlHelper, expression, selectList, optionLabel, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes), cols));
 }
Example #2
0
 public static MvcHtmlString CheckBoxListFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, IEnumerable <SelectListItem> selectList, string optionLabel, ColNumber cols)
 {
     return(CheckBoxListFor(htmlHelper, expression, selectList, optionLabel, null /* htmlAttributes */, cols));
 }
Example #3
0
        public static StringBuilder BuildItems(string optionLabel, IEnumerable <SelectListItem> selectList, IDictionary <string, object> htmlAttributes, string fullName, ColNumber cols)
        {
            if (cols == ColNumber.twoCols)
            {
                var listItemBuilder = new StringBuilder();

                // Make optionLabel the first item that gets rendered.

                listItemBuilder.AppendLine("<div class=\"row\">");
                listItemBuilder.AppendLine("<div class=\"col-md-6\">");

                var    selectListItems = selectList as SelectListItem[] ?? selectList.ToArray();
                int    length          = selectListItems.Length;
                double halfLength      = ((double)length) / 2.0;
                int    index           = 0;
                if (optionLabel != null)
                {
                    listItemBuilder.AppendLine(ListItemToCheckbox(new SelectListItem {
                        Text = optionLabel, Value = String.Empty, Selected = false
                    }, htmlAttributes, fullName));
                    halfLength = ((double)(length - 1.0)) / 2.0;
                }

                for (; index < halfLength; index++)
                {
                    listItemBuilder.AppendLine(ListItemToCheckbox(selectListItems.ElementAt(index), htmlAttributes, fullName));
                }
                listItemBuilder.AppendLine("</div>");
                listItemBuilder.AppendLine("<div class=\"col-md-6\">");
                for (; index < length; index++)
                {
                    listItemBuilder.AppendLine(ListItemToCheckbox(selectListItems.ElementAt(index), htmlAttributes, fullName));
                }
                listItemBuilder.AppendLine("</div>");
                listItemBuilder.AppendLine("</div>");
                return(listItemBuilder);
            }

            return(BuildItems(optionLabel, selectList, htmlAttributes, fullName));
        }
Example #4
0
        public static MvcHtmlString CheckboxListInternal(this HtmlHelper htmlHelper, ModelMetadata metadata,
                                                         string optionLabel, string name, IEnumerable <SelectListItem> selectList, IDictionary <string, object> htmlAttributes, ColNumber cols)
        {
            var fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);

            if (String.IsNullOrEmpty(fullName))
            {
                throw new ArgumentNullException("name", "Value cannot be null or empty.");
            }

            var defaultValue = GetModelStateValue(htmlHelper, fullName, typeof(string[]));

            // If we haven't already used ViewData to get the entire list of items then we need to
            // use the ViewData-supplied value before using the parameter-supplied value.
            if (defaultValue == null && !String.IsNullOrEmpty(name))
            {
                defaultValue = htmlHelper.ViewData.Eval(name);
            }

            if (defaultValue != null)
            {
                selectList = GetSelectListWithDefaultValue(selectList, defaultValue);
            }

            // Convert each ListItem to an <option> tag and wrap them with <optgroup> if requested.
            var listItemBuilder = BuildItems(optionLabel, selectList, htmlAttributes, fullName, cols);

            var tagBuilder = new TagBuilder("div")
            {
                InnerHtml = listItemBuilder.ToString()
            };

            tagBuilder.GenerateId(fullName);

            // If there are any errors for a named field, we add the css attribute.
            ModelState modelState;

            if (htmlHelper.ViewData.ModelState.TryGetValue(fullName, out modelState))
            {
                if (modelState.Errors.Count > 0)
                {
                    tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
                }
            }

            tagBuilder.MergeAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata));
            return(MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal)));
        }
Example #5
0
 private static MvcHtmlString CheckBoxListHelper(HtmlHelper htmlHelper, ModelMetadata metadata, string expression, IEnumerable <SelectListItem> selectList, string optionLabel, IDictionary <string, object> htmlAttributes, ColNumber cols)
 {
     return(CheckboxListInternal(htmlHelper, metadata, optionLabel, expression, selectList, htmlAttributes, cols));
 }
Example #6
0
        public static MvcHtmlString CheckBoxListFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, IEnumerable <SelectListItem> selectList, string optionLabel, IDictionary <string, object> htmlAttributes, ColNumber cols)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);

            return(CheckBoxListHelper(htmlHelper, metadata, ExpressionHelper.GetExpressionText(expression), selectList, optionLabel, htmlAttributes, cols));
        }