Example #1
0
        private static IHtmlContent RenderBootstrapFormCheckGroup <TModel, TResult>(this IHtmlHelper <TModel> helper,
                                                                                    Expression <Func <TModel, TResult> > expression,
                                                                                    FormCheckType formCheckType,
                                                                                    IEnumerable <SelectListItem> selectListItems,
                                                                                    LayoutDirection layoutDirection = LayoutDirection.Horizontal,
                                                                                    object?htmlAttributes           = null)
        {
            var result = new HtmlContentBuilder();

            foreach (var item in selectListItems)
            {
                var inputTag = new TagBuilder("input")
                {
                    TagRenderMode = TagRenderMode.SelfClosing
                };

                inputTag.Attributes.Add("id", "_" + Crypto.RandomString());
                inputTag.Attributes.Add("name", helper.NameFor(expression));
                inputTag.Attributes.Add("value", item.Value);
                inputTag.Attributes.Add("type", formCheckType == FormCheckType.Switch ? "checkbox" : formCheckType.ToLower());
                if (item.Selected)
                {
                    inputTag.Attributes.Add("checked", "checked");
                }
                inputTag.MergeAttributes(htmlAttributes);

                var additionalCssClass = (layoutDirection == LayoutDirection.Horizontal ? "form-check-inline custom-control-inline" : "");
                var formCheck          = BootstrapFormCheck(inputTag, formCheckType, item.Text, additionalCssClass);

                result.AppendHtml(formCheck);
            }
            return(result);
        }
Example #2
0
        private static string BootstrapFormCheck(TagBuilder inputTag, FormCheckType formCheckType, string?label, string?additionalCssClass = null)
        {
            if (!inputTag.Attributes.TryGetValue("class", out var cssClass) || !cssClass.Contains("form-check-input"))
            {
                inputTag.AddCssClass("form-check-input");
                inputTag.AddCssClass("custom-control-input");
            }
            return($@"<div class='form-check {(formCheckType == FormCheckType.Switch ? "form-switch" : "")} custom-control custom-{formCheckType.ToLower()} {additionalCssClass}'>
				{inputTag.ToHtml()}
				<label class='form-check-label custom-control-label' for='{inputTag.Attributes.GetValueOrDefault("id")}'>{label}</span>
			</div>"            );
        }