コード例 #1
0
ファイル: HtmlHelperExtensions.cs プロジェクト: cwx521/Husky
        private static IHtmlContent RenderBootstrapFormCheck <TModel>(this IHtmlHelper <TModel> helper,
                                                                      Expression <Func <TModel, bool> > expression,
                                                                      FormCheckType formCheckType = FormCheckType.CheckBox,
                                                                      string?label = null,
                                                                      string?additionalCssClass = null)
        {
            var inputTag = new TagBuilder("input")
            {
                TagRenderMode = TagRenderMode.SelfClosing
            };

            inputTag.Attributes.Add("id", helper.IdFor(expression));
            inputTag.Attributes.Add("name", helper.NameFor(expression));
            inputTag.Attributes.Add("type", "checkbox");
            inputTag.Attributes.Add("value", "true");

            if (helper.ViewData.Model != null)
            {
                try {
                    var value = expression.Compile().Invoke(helper.ViewData.Model);
                    if (value)
                    {
                        inputTag.Attributes.Add("checked", "checked");
                    }
                }
                catch (NullReferenceException) { }
                catch { throw; }
            }

            var formCheck = BootstrapFormCheck(inputTag, formCheckType, label ?? helper.DisplayNameFor(expression), additionalCssClass);

            return(new HtmlString(formCheck));
        }
コード例 #2
0
ファイル: HtmlHelperExtensions.cs プロジェクト: cwx521/Husky
        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);
        }
コード例 #3
0
ファイル: HtmlHelperExtensions.cs プロジェクト: cwx521/Husky
        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>"            );
        }