public static string RenderFormGroupCustom(HtmlHelper html, FormGroupCustomModel customModel, LabelModel labelModel)
        {
            var label = RenderControl.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();
            }

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

            bool fieldIsValid = true;

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

            return new FormGroup(htmlString, label, FormGroupType.TextBoxLike, fieldIsValid).ToHtmlString();
        }
        public static string RenderFormGroupRadioButton(HtmlHelper html, RadioButtonModel inputModel, LabelModel labelModel)
        {
            string validationMessage = "";
            string radioType = "form-icon";

            if (inputModel.displayValidationMessage)
            {
                string 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 = RenderControl.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()
            });

            bool fieldIsValid = true;

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

            return new FormGroup(null, label, FormGroupType.CheckBoxLike, fieldIsValid).ToHtmlString();
        }
Esempio n. 3
0
        public static HtmlString RenderLabel(HtmlHelper html, LabelModel model)
        {
            string fullHtmlFieldName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(model.htmlFieldName);
            string innerinput = "";

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

                    //label.AddOrMergeCssClass("checkbox");
                    inputModel.displayValidationMessage = false;
                    model.innerInput = MvcHtmlString.Create(inputModel.isSingleInput
                        ? RenderControl.RenderCheckBoxCustom(html, inputModel).ToHtmlString()
                        : RenderControl.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)
                {
                    RadioButtonModel inputModel = (RadioButtonModel)model.innerInputModel;

                    model.innerInput = MvcHtmlString.Create(RenderControl.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.ToString() + model.innerValidationMessage;

            return new HtmlString(label.ToString(TagRenderMode.Normal));
        }
        public static string RenderFormGroupTextBox(HtmlHelper html, TextBoxModel inputModel, LabelModel labelModel)
        {
            var input = RenderControl.RenderTextBox(html, inputModel, false);
            var label = RenderControl.RenderLabel(html, labelModel ?? new LabelModel
            {
                htmlFieldName = inputModel.htmlFieldName,
                metadata = inputModel.metadata,
                htmlAttributes = new { @class = "control-label" }.ToDictionary()
            });

            bool fieldIsValid = true;

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

            return new FormGroup(input, label, FormGroupType.TextBoxLike, fieldIsValid).ToHtmlString();
        }
        public static string RenderFormGroupSelectElement(HtmlHelper html, SelectElementModel inputModel, LabelModel labelModel, InputType inputType)
        {
            HtmlString input = null;

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

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

            var label = RenderControl.RenderLabel(html, labelModel ?? new LabelModel
            {
                htmlFieldName = inputModel.htmlFieldName,
                metadata = inputModel.metadata,
                htmlAttributes = new { @class = "control-label" }.ToDictionary()
            });

            bool fieldIsValid = true;

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

            return new FormGroup(input, label, FormGroupType.TextBoxLike, fieldIsValid).ToHtmlString();
        }