public static string RenderControlGroupCheckBox(HtmlHelper html, BootstrapCheckBoxModel inputModel, BootstrapLabelModel labelModel)
        {
            if (string.IsNullOrEmpty(inputModel.htmlFieldName))
            {
                return(null);
            }

            string validationMessage = "";

            if (inputModel.displayValidationMessage)
            {
                string validation = html.ValidationMessage(inputModel.htmlFieldName).ToHtmlString();
                validationMessage = new BootstrapHelpText(validation, inputModel.validationMessageStyle).ToHtmlString();
            }

            string label = Renderer.RenderLabel(html, labelModel ?? new BootstrapLabelModel
            {
                htmlFieldName          = inputModel.htmlFieldName,
                metadata               = inputModel.metadata,
                innerInputType         = BootstrapInputType.CheckBox,
                innerInputModel        = inputModel,
                innerValidationMessage = validationMessage
            });

            bool fieldIsValid = true;

            if (inputModel != null)
            {
                fieldIsValid = html.ViewData.ModelState.IsValidField(inputModel.htmlFieldName);
            }
            return(new BootstrapControlGroup(null, label, ControlGroupType.checkboxLike, fieldIsValid).ToHtmlString());
        }
Пример #2
0
        public static string RenderCheckBox(HtmlHelper html, BootstrapCheckBoxModel model)
        {
            if (model.tooltipConfiguration != null)
            {
                model.htmlAttributes.AddRange(model.tooltipConfiguration.ToDictionary());
            }
            var mergedHtmlAttrs = string.IsNullOrEmpty(model.id) ? model.htmlAttributes : model.htmlAttributes.AddOrReplace("id", model.id);

            string validationMessage = "";

            if (model.displayValidationMessage)
            {
                string validation = html.ValidationMessage(model.htmlFieldName).ToHtmlString();
                validationMessage = new BootstrapHelpText(validation, model.validationMessageStyle).ToHtmlString();
            }
            return(html.CheckBox(model.htmlFieldName, model.isChecked, mergedHtmlAttrs.FormatHtmlAttributes()).ToHtmlString() + validationMessage);
        }
Пример #3
0
        public static string RenderCheckBoxCustom(HtmlHelper html, BootstrapCheckBoxModel model)
        {
            string fullHtmlFieldName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(model.htmlFieldName);

            model.htmlAttributes.MergeHtmlAttributes(html.GetUnobtrusiveValidationAttributes(model.htmlFieldName, model.metadata));
            if (model.tooltipConfiguration != null)
            {
                model.htmlAttributes.MergeHtmlAttributes(model.tooltipConfiguration.ToDictionary());
            }
            if (model.tooltip != null)
            {
                model.htmlAttributes.MergeHtmlAttributes(model.tooltip.ToDictionary());
            }

            ModelState modelState;

            if (html.ViewData.ModelState.TryGetValue(fullHtmlFieldName, out modelState))
            {
                if (modelState.Errors.Count > 0)
                {
                    model.htmlAttributes.AddOrMergeCssClass("class", "input-validation-error");
                }
                if (modelState.Value != null && ((string[])modelState.Value.RawValue).Contains(model.value.ToString()))
                {
                    model.isChecked = true;
                }
            }

            TagBuilder input = new TagBuilder("input");

            input.Attributes.Add("type", "checkbox");
            input.Attributes.Add("name", fullHtmlFieldName);
            input.Attributes.Add("id", model.id);
            input.Attributes.Add("value", model.value.ToString());
            if (model.isChecked)
            {
                input.Attributes.Add("checked", "checked");
            }
            if (model.isDisabled)
            {
                input.Attributes.Add("disabled", "disabled");
            }
            input.MergeAttributes(model.htmlAttributes.FormatHtmlAttributes());

            return(input.ToString(TagRenderMode.SelfClosing));
        }
Пример #4
0
        public static string RenderLabel(HtmlHelper html, BootstrapLabelModel model)
        {
            string fullHtmlFieldName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(model.htmlFieldName);

            if (string.IsNullOrEmpty(model.labelText))
            {
                model.labelText = model.metadata.DisplayName
                                  ?? (model.metadata.PropertyName != null ? model.metadata.PropertyName.SplitByUpperCase() : null)
                                  ?? fullHtmlFieldName.Split('.').Last().SplitByUpperCase();
            }

            TagBuilder label = new TagBuilder("label");

            label.Attributes.Add("for", fullHtmlFieldName.FormatForMvcInputId() + (model.index.HasValue ? "_" + model.index.Value.ToString() : string.Empty));
            label.MergeAttributes(model.htmlAttributes.FormatHtmlAttributes());

            TagBuilder requiredSpan = new TagBuilder("span");

            requiredSpan.AddCssClass("required");
            requiredSpan.SetInnerText("*");
            if ((model.showRequiredStar.HasValue && !model.showRequiredStar.Value) || (!model.showRequiredStar.HasValue && !model.metadata.IsRequired))
            {
                requiredSpan.AddCssStyle("visibility", "hidden");
            }

            if (model.innerInputType != BootstrapInputType._NotSet)
            {
                if (model.innerInputType == BootstrapInputType.CheckBox)
                {
                    label.AddOrMergeCssClass("checkbox");
                    BootstrapCheckBoxModel inputModel = (BootstrapCheckBoxModel)model.innerInputModel;
                    inputModel.displayValidationMessage = false;
                    model.innerInput = MvcHtmlString.Create(inputModel.isSingleInput
                        ? Renderer.RenderCheckBoxCustom(html, inputModel)
                        : Renderer.RenderCheckBox(html, inputModel));
                    if (inputModel.htmlAttributes.Keys.Select(x => x.ToLower()).Contains("id"))
                    {
                        label.Attributes["for"] = inputModel.htmlAttributes["id"].ToString();
                    }
                }
                if (model.innerInputType == BootstrapInputType.Radio)
                {
                    label.AddOrMergeCssClass("radio");
                    BootstrapRadioButtonModel inputModel = (BootstrapRadioButtonModel)model.innerInputModel;
                    model.innerInput = MvcHtmlString.Create(Renderer.RenderRadioButton(html, inputModel));
                    if (inputModel.htmlAttributes.Keys.Select(x => x.ToLower()).Contains("id"))
                    {
                        label.Attributes["for"] = inputModel.htmlAttributes["id"].ToString();
                    }
                }
            }

            string innerinput = "";

            if (model.innerInput != null)
            {
                innerinput = model.innerInput.ToHtmlString();
            }

            label.InnerHtml = innerinput + model.labelText + requiredSpan.ToString() + model.innerValidationMessage;

            return(label.ToString(TagRenderMode.Normal));
        }
Пример #5
0
        public static string RenderInputListItem(
            HtmlHelper html,
            BootstrapInputType inputType,
            string htmlFieldName,
            ModelMetadata metadata,
            int index,
            string inputValue,
            string inputText,
            object inputHtmlAttributes,
            object labelHtmlAttributes,
            bool inputIsChecked,
            bool inputIsDisabled)
        {
            string input             = string.Empty;
            string fullHtmlFieldName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName);
            var    htmlAttrs         = labelHtmlAttributes.ToDictionary();

            switch (inputType)
            {
            case BootstrapInputType._NotSet:
                break;

            case BootstrapInputType.CheckBoxList:
            {
                htmlAttrs.AddOrMergeCssClass("class", "checkbox").FormatHtmlAttributes();

                BootstrapCheckBoxModel checkboxModel = new BootstrapCheckBoxModel
                {
                    htmlFieldName  = htmlFieldName,
                    value          = inputValue,
                    metadata       = metadata,
                    htmlAttributes = inputHtmlAttributes.ToDictionary().FormatHtmlAttributes(),
                    id             = fullHtmlFieldName.FormatForMvcInputId() + "_" + index.ToString(),
                    isChecked      = inputIsChecked,
                    isDisabled     = inputIsDisabled
                };

                input = Renderer.RenderCheckBoxCustom(html, checkboxModel);
                break;
            }

            case BootstrapInputType.RadioList:
            {
                htmlAttrs.AddOrMergeCssClass("class", "radio").FormatHtmlAttributes();

                BootstrapRadioButtonModel radiobuttonModel = new BootstrapRadioButtonModel
                {
                    htmlFieldName  = htmlFieldName,
                    value          = inputValue,
                    metadata       = metadata,
                    htmlAttributes = inputHtmlAttributes.ToDictionary().FormatHtmlAttributes(),
                    id             = fullHtmlFieldName.FormatForMvcInputId() + "_" + index.ToString(),
                    isChecked      = inputIsChecked,
                    isDisabled     = inputIsDisabled
                };

                input = Renderer.RenderRadioButton(html, radiobuttonModel);
                break;
            }

            default:
                break;
            }

            BootstrapLabelModel labelModel = new BootstrapLabelModel
            {
                index            = index,
                htmlFieldName    = htmlFieldName,
                labelText        = inputText,
                metadata         = metadata,
                htmlAttributes   = htmlAttrs,
                innerInput       = MvcHtmlString.Create(input),
                showRequiredStar = false
            };

            string labeledInput = Renderer.RenderLabel(html, labelModel);

            return(labeledInput);
        }
Пример #6
0
        public static string RenderCheckBoxCustom(HtmlHelper html, BootstrapCheckBoxModel model)
        {
            var fullHtmlFieldName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(model.htmlFieldName);

            model.htmlAttributes.MergeHtmlAttributes(html.GetUnobtrusiveValidationAttributes(model.htmlFieldName, model.metadata));
            if (model.tooltipConfiguration != null)
            {
                model.htmlAttributes.MergeHtmlAttributes(model.tooltipConfiguration.ToDictionary());
            }
            if (model.tooltip != null)
            {
                model.htmlAttributes.MergeHtmlAttributes(model.tooltip.ToDictionary());
            }

            ModelState modelState;

            if (html.ViewData.ModelState.TryGetValue(fullHtmlFieldName, out modelState))
            {
                if (modelState.Errors.Count > 0)
                {
                    model.htmlAttributes.AddOrMergeCssClass("class", "input-validation-error");
                }
                if (modelState.Value != null && ((string[])modelState.Value.RawValue).Contains("True"))
                {
                    model.isChecked = true;
                }
            }

            var checkBoxDiv = new TagBuilder("div");

            checkBoxDiv.MergeAttribute("class", "checkbox");

            var label = new TagBuilder("label");

            var input = html.CheckBox(model.htmlFieldName, model.isChecked, model.htmlAttributes).ToString();

            var span = new TagBuilder("span");

            span.MergeAttribute("class", "text");
            span.InnerHtml = !string.IsNullOrEmpty(model.metadata.DisplayName) ? model.metadata.DisplayName : model.text;

            label.InnerHtml       = input + span.ToString();
            checkBoxDiv.InnerHtml = label.ToString();

            var widthlg = "";

            if (model.InputWidthLg != 0)
            {
                var width = model.InputWidthLg.ToString();
                widthlg = " col-lg-" + width;
            }

            var widthMd = "";

            if (model.InputWidthMd != 0)
            {
                var width = model.InputWidthMd.ToString();
                widthMd = " col-md-" + width;
            }

            var widthSm = "";

            if (model.InputWidthSm != 0)
            {
                var width = model.InputWidthSm.ToString();
                widthSm = " col-sm-" + width;
            }

            var widthXs = "";

            if (model.InputWidthXs != 0)
            {
                var width = model.InputWidthXs.ToString();
                widthXs = " col-xs-" + width;
            }

            var offsetlg = "";

            if (model.InputOffsetLg != 0)
            {
                var offset = model.InputOffsetLg.ToString();
                offsetlg = " col-lg-offset-" + offset;
            }

            var offsetMd = "";

            if (model.InputOffsetMd != 0)
            {
                var offset = model.InputOffsetMd.ToString();
                offsetMd = " col-md-offset-" + offset;
            }

            var offsetSm = "";

            if (model.InputOffsetSm != 0)
            {
                var offset = model.InputOffsetSm.ToString();
                offsetSm = " col-sm-offset-" + offset;
            }

            var offsetXs = "";

            if (model.InputOffsetXs != 0)
            {
                var offset = model.InputOffsetXs.ToString();
                offsetXs = " col-xs-offset-" + offset;
            }

            checkBoxDiv.AddOrMergeCssClass(widthlg + widthMd + widthSm + widthXs);
            checkBoxDiv.AddOrMergeCssClass(offsetlg + offsetMd + offsetSm + offsetXs);


            return(checkBoxDiv.ToString());
        }
        public static string RenderFormGroupCheckBox(HtmlHelper html, BootstrapCheckBoxModel inputModel, BootstrapLabelModel labelModel)
        {
            var input = Renderer.RenderCheckBoxCustom(html, inputModel);


            string validationMessage = "";

            if (inputModel.displayValidationMessage)
            {
                string validation = html.ValidationMessage(inputModel.htmlFieldName).ToHtmlString();
                validationMessage = new BootstrapHelpText(validation, inputModel.validationMessageStyle).ToHtmlString();
            }

            var widthlg = "";

            if (inputModel.LabelWidthLg != 0)
            {
                var width = inputModel.LabelWidthLg.ToString();
                widthlg = " col-lg-" + width;
            }

            var widthMd = "";

            if (inputModel.LabelWidthMd != 0)
            {
                var width = inputModel.LabelWidthMd.ToString();
                widthMd = " col-md-" + width;
            }

            var widthSm = "";

            if (inputModel.LabelWidthSm != 0)
            {
                var width = inputModel.LabelWidthSm.ToString();
                widthSm = " col-sm-" + width;
            }

            var widthXs = "";

            if (inputModel.LabelWidthXs != 0)
            {
                var width = inputModel.LabelWidthXs.ToString();
                widthXs = " col-xs-" + width;
            }

            var offsetlg = "";

            if (inputModel.LabelOffsetLg != 0)
            {
                var offset = inputModel.LabelOffsetLg.ToString();
                offsetlg = " col-lg-offset-" + offset;
            }

            var offsetMd = "";

            if (inputModel.LabelOffsetMd != 0)
            {
                var offset = inputModel.LabelOffsetMd.ToString();
                offsetMd = " col-md-offset-" + offset;
            }

            var offsetSm = "";

            if (inputModel.LabelOffsetSm != 0)
            {
                var offset = inputModel.LabelOffsetSm.ToString();
                offsetSm = " col-sm-offset-" + offset;
            }

            var offsetXs = "";

            if (inputModel.LabelOffsetXs != 0)
            {
                var offset = inputModel.LabelOffsetXs.ToString();
                offsetXs = " col-xs-offset-" + offset;
            }

            string label = Renderer.RenderLabel(html, labelModel ?? new BootstrapLabelModel
            {
                htmlFieldName          = inputModel.htmlFieldName,
                metadata               = inputModel.metadata,
                innerInputType         = BootstrapInputType.CheckBox,
                innerInputModel        = inputModel,
                innerValidationMessage = validationMessage,
                htmlAttributes         = new { @class = widthlg + widthMd + widthSm + widthXs + offsetlg + offsetMd + offsetSm + offsetXs }.ToDictionary()
            });

            bool fieldIsValid = true;

            if (inputModel != null)
            {
                fieldIsValid = html.ViewData.ModelState.IsValidField(inputModel.htmlFieldName);
            }
            return(new BootstrapFormGroup(input, null, FormGroupType.textboxLike, fieldIsValid).ToHtmlString());
        }