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()); 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); }
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; }
public static string RenderControlGroupCheckBox(HtmlHelper html, BootstrapCheckBoxModel inputModel, BootstrapLabelModel labelModel) { 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(); }
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; }