public static string RenderFormGroupRadioButtonPair(HtmlHelper html, RadioButtonPairModel inputModel, LabelModel labelModel) { var input = RenderRadioButtonPair(html, inputModel); var label = RenderLabel(html, labelModel ?? new LabelModel { htmlFieldName = inputModel.htmlFieldName, metadata = inputModel.metadata, htmlAttributes = new {@class = "control-label"}.ToDictionary() }); var fieldIsValid = true; if (inputModel != null) fieldIsValid = html.ViewData.ModelState.IsValidField(inputModel.htmlFieldName); return new FormGroup(input, label, FormGroupType.TextBoxLike, fieldIsValid).ToHtmlString(); }
public static HtmlString RenderRadioButtonPair(HtmlHelper html, RadioButtonPairModel model) { TagBuilder wrapper = null; var inputPairs = string.Empty; var fullHtmlFieldName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(model.htmlFieldName); var radioType = "form-icon"; var pairOneValueIsSelected = false; var pairTwoValueIsSelected = false; if (model.metadata.Model != null) { pairOneValueIsSelected = model.inputPairOneValue.ToString() == model.metadata.Model.ToString(); pairTwoValueIsSelected = model.inputPairOneValue.ToString() != model.metadata.Model.ToString(); } if (model.RadioType != InputRadioCheckBoxType._NotSet) radioType = model.RadioType.ToName(); var inputPairOne = RenderLabel(html, new LabelModel { htmlFieldName = model.htmlFieldName, labelText = model.labelPairOneText, metadata = model.metadata, htmlAttributes = model.htmlAttributesLabelPairOne.AddOrMergeCssClass("class", $"form-radio {radioType} form-text"), showRequiredStar = false, innerInputType = InputType.Radio, innerInputModel = new RadioButtonModel { htmlFieldName = model.htmlFieldName, value = model.inputPairOneValue, metadata = model.metadata, isChecked = pairOneValueIsSelected, RadioType = model.RadioType, htmlAttributes = model.htmlAttributesInputPairOne.AddOrReplace("id", fullHtmlFieldName.FormatForMvcInputId() + "_pairOne") } }); var inputPairTwo = RenderLabel(html, new LabelModel { htmlFieldName = model.htmlFieldName, labelText = model.labelPairTwoText, metadata = model.metadata, htmlAttributes = model.htmlAttributesLabelPairTwo.AddOrMergeCssClass("class", $"form-radio {radioType} form-text"), showRequiredStar = false, innerInputType = InputType.Radio, innerInputModel = new RadioButtonModel { htmlFieldName = model.htmlFieldName, value = model.inputPairTwoValue, metadata = model.metadata, isChecked = pairTwoValueIsSelected, RadioType = model.RadioType, htmlAttributes = model.htmlAttributesInputPairTwo.AddOrReplace("id", fullHtmlFieldName.FormatForMvcInputId() + "_pairTwo") } }); var helpText = model.helpText != null ? model.helpText.ToHtmlString() : string.Empty; var validationMessage = ""; if (model.displayValidationMessage) { var validation = html.ValidationMessage(model.htmlFieldName).ToHtmlString(); validationMessage = new HelpText(validation, model.validationMessageStyle).ToHtmlString(); } if (model.RadioStyle == CheckBoxRadioStyle.Block) { var wrapperPairOne = new TagBuilder("div"); var wrapperPairTwo = new TagBuilder("div"); wrapperPairOne.AddCssClass("radio"); wrapperPairOne.InnerHtml = inputPairOne.ToHtmlString(); wrapperPairTwo.AddCssClass("radio"); wrapperPairTwo.InnerHtml = inputPairTwo.ToHtmlString(); inputPairs = wrapperPairOne + wrapperPairTwo.ToString(); } else if (model.RadioStyle == CheckBoxRadioStyle.Inline) { var inputsWrapper = new TagBuilder("div"); inputsWrapper.AddCssClass("radio"); inputsWrapper.InnerHtml = inputPairOne.ToHtmlString() + inputPairTwo.ToHtmlString(); inputPairs = inputsWrapper.ToString(); } if (!string.IsNullOrEmpty(model.radioPairElementWrapper)) { wrapper = new TagBuilder(model.radioPairElementWrapper); if (model.radioPairElementWrapperAttributes != null) wrapper.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(model.radioPairElementWrapperAttributes)); wrapper.InnerHtml = inputPairs; } var htmlString = wrapper != null ? wrapper.ToString(TagRenderMode.Normal) : inputPairs; return new HtmlString(htmlString); }