public static string RenderFormGroupRadioButton(HtmlHelper html, RadioButtonModel inputModel, LabelModel labelModel)
        {
            var validationMessage = "";
            var radioType = "form-icon";

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

            if (labelModel == null && inputModel.RadioType != InputRadioCheckBoxType._NotSet)
                radioType = inputModel.RadioType.ToName();

            var label = RenderLabel(html, labelModel ?? new LabelModel
            {
                htmlFieldName = inputModel.htmlFieldName,
                metadata = inputModel.metadata,
                innerInputType = InputType.Radio,
                innerInputModel = inputModel,
                innerValidationMessage = validationMessage,
                htmlAttributes = new {@class = $"form-radio {radioType} 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 string RenderFormGroupSelectElement(HtmlHelper html, SelectElementModel inputModel,
			LabelModel labelModel, InputType inputType)
        {
            HtmlString input = null;

            if (inputType == InputType.DropDownList)
                input = RenderSelectElement(html, inputModel, InputType.DropDownList);

            if (inputType == InputType.ListBox)
                input = RenderSelectElement(html, inputModel, InputType.ListBox);

            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 string RenderFormGroupCustom(HtmlHelper html, FormGroupCustomModel customModel, LabelModel labelModel)
        {
            var label = RenderLabel(html, labelModel ?? new LabelModel
            {
                htmlFieldName = labelModel.htmlFieldName,
                metadata = labelModel.metadata,
                htmlAttributes = new {@class = "control-label"}.ToDictionary()
            });

            var htmlInput = customModel.input;

            TagBuilder wrapper = null;

            if (!string.IsNullOrEmpty(customModel.controlsElementWrapper))
            {
                wrapper = new TagBuilder(customModel.controlsElementWrapper);

                if (customModel.controlsElementWrapperAttributes != null)
                    wrapper.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(customModel.controlsElementWrapperAttributes));

                wrapper.InnerHtml = htmlInput.ToHtmlString();
            }

            var htmlString = wrapper != null
                ? new HtmlString(wrapper.ToString(TagRenderMode.Normal))
                : htmlInput;

            var fieldIsValid = true;

            if (labelModel != null && labelModel.htmlFieldName != null)
                fieldIsValid = html.ViewData.ModelState.IsValidField(labelModel.htmlFieldName);

            return new FormGroup(htmlString, label, FormGroupType.TextBoxLike, fieldIsValid).ToHtmlString();
        }
Ejemplo n.º 4
0
        public static HtmlString RenderLabel(HtmlHelper html, LabelModel model)
        {
            var fullHtmlFieldName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(model.htmlFieldName);
            var innerinput = "";

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

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

            var 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 != InputType._NotSet)
            {
                if (model.innerInputType == InputType.CheckBox)
                {
                    var inputModel = (CheckBoxModel) model.innerInputModel;

                    //label.AddOrMergeCssClass("checkbox");
                    inputModel.displayValidationMessage = false;
                    model.innerInput = MvcHtmlString.Create(inputModel.isSingleInput
                        ? RenderCheckBoxCustom(html, inputModel).ToHtmlString()
                        : RenderCheckBox(html, inputModel).ToHtmlString());

                    if (inputModel.htmlAttributes.Keys.Select(x => x.ToLower()).Contains("id"))
                        label.Attributes["for"] = inputModel.htmlAttributes["id"].ToString();
                }
                if (model.innerInputType == InputType.Radio)
                {
                    var inputModel = (RadioButtonModel) model.innerInputModel;

                    model.innerInput = MvcHtmlString.Create(RenderRadioButton(html, inputModel).ToHtmlString());

                    if (inputModel.htmlAttributes.Keys.Select(x => x.ToLower()).Contains("id"))
                        label.Attributes["for"] = inputModel.htmlAttributes["id"].ToString();
                }
            }

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

            label.InnerHtml = innerinput + model.labelText + requiredSpan + model.innerValidationMessage;

            return new HtmlString(label.ToString(TagRenderMode.Normal));
        }
        public static string RenderFormGroupTextBox(HtmlHelper html, TextBoxModel inputModel, LabelModel labelModel)
        {
            var input = RenderTextBox(html, inputModel, false);
            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 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;
        }