public static string RenderFormGroupCheckBox(HtmlHelper html, CheckBoxModel inputModel, LabelModel labelModel) { var validationMessage = ""; var chkType = "form-icon"; if (inputModel.displayValidationMessage) { var validation = html.ValidationMessage(inputModel.htmlFieldName).ToHtmlString(); validationMessage = new HelpText(validation, inputModel.validationMessageStyle).ToHtmlString(); } if (labelModel == null && inputModel.CheckBoxType != InputRadioCheckBoxType._NotSet) chkType = inputModel.CheckBoxType.ToName(); var label = RenderLabel(html, labelModel ?? new LabelModel { htmlFieldName = inputModel.htmlFieldName, metadata = inputModel.metadata, innerInputType = InputType.CheckBox, innerInputModel = inputModel, innerValidationMessage = validationMessage, htmlAttributes = new {@class = $"form-checkbox {chkType} form-text"}.ToDictionary() }); var fieldIsValid = true; if (inputModel != null) fieldIsValid = html.ViewData.ModelState.IsValidField(inputModel.htmlFieldName); return new FormGroup(null, label, FormGroupType.CheckBoxLike, fieldIsValid).ToHtmlString(); }
public static HtmlString RenderCheckBoxCustom(HtmlHelper html, CheckBoxModel model) { var fullHtmlFieldName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(model.htmlFieldName); model.htmlAttributes.MergeHtmlAttributes(html.GetUnobtrusiveValidationAttributes(model.htmlFieldName, model.metadata)); 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; } var 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 new HtmlString(input.ToString(TagRenderMode.SelfClosing)); }
public static HtmlString RenderCheckBox(HtmlHelper html, CheckBoxModel model) { model.htmlAttributes.MergeHtmlAttributes(html.GetUnobtrusiveValidationAttributes(model.htmlFieldName, model.metadata)); var mergedHtmlAttrs = string.IsNullOrEmpty(model.id) ? model.htmlAttributes : model.htmlAttributes.AddOrReplace("id", model.id); var validationMessage = ""; if (model.displayValidationMessage) { var validation = html.ValidationMessage(model.htmlFieldName).ToHtmlString(); validationMessage = new HelpText(validation, model.validationMessageStyle).ToHtmlString(); } return new HtmlString( html.CheckBox(model.htmlFieldName, model.isChecked, mergedHtmlAttrs.FormatHtmlAttributes()).ToHtmlString() + validationMessage); }
public static string RenderInputListItem(HtmlHelper html, InputType inputType, string htmlFieldName, ModelMetadata metadata, int index, string inputValue, string inputText, object inputHtmlAttributes, object labelHtmlAttributes, bool inputIsChecked, bool inputIsDisabled, InputRadioCheckBoxType inputItemType) { var input = string.Empty; var fullHtmlFieldName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName); var htmlAttrs = labelHtmlAttributes.ToDictionary(); var itemType = "form-icon"; if (inputItemType != InputRadioCheckBoxType._NotSet) itemType = inputItemType.ToName(); switch (inputType) { case InputType._NotSet: break; case InputType.CheckBoxList: { htmlAttrs.AddOrMergeCssClass("class", $"form-checkbox {itemType} form-text").FormatHtmlAttributes(); var checkboxModel = new CheckBoxModel { htmlFieldName = htmlFieldName, value = inputValue, metadata = metadata, htmlAttributes = inputHtmlAttributes.ToDictionary().FormatHtmlAttributes(), id = fullHtmlFieldName.FormatForMvcInputId() + "_" + index, isChecked = inputIsChecked, isDisabled = inputIsDisabled }; input = RenderCheckBoxCustom(html, checkboxModel).ToHtmlString(); break; } case InputType.RadioList: { htmlAttrs.AddOrMergeCssClass("class", $"form-radio {itemType} form-text").FormatHtmlAttributes(); var radiobuttonModel = new RadioButtonModel { htmlFieldName = htmlFieldName, value = inputValue, metadata = metadata, htmlAttributes = inputHtmlAttributes.ToDictionary().FormatHtmlAttributes(), id = fullHtmlFieldName.FormatForMvcInputId() + "_" + index, isChecked = inputIsChecked, isDisabled = inputIsDisabled }; input = RenderRadioButton(html, radiobuttonModel).ToHtmlString(); break; } default: break; } var labelModel = new LabelModel { index = index, htmlFieldName = htmlFieldName, labelText = inputText, metadata = metadata, htmlAttributes = htmlAttrs, innerInput = MvcHtmlString.Create(input), showRequiredStar = false }; var labeledInput = RenderLabel(html, labelModel).ToHtmlString(); return labeledInput; }