Example #1
0
        public static LiteralTag ValidationSummary(this HtmlHelper htmlHelper, bool excludePropertyErrors)
        {
            var val = ValidationExtensions.ValidationSummary(htmlHelper, excludePropertyErrors);

            if (val != null)
            {
                return(new LiteralTag(val.ToHtmlString()));
            }

            var valtag = new DivTag().AddClass(HtmlHelper.ValidationSummaryCssClassName);

            valtag.Append(new HtmlTag("ul").Append(new HtmlTag("li").Style("display", "none")));
            if (!excludePropertyErrors)
            {
                valtag.Data("valmsg-summary", "true");
            }

            return(new LiteralTag(valtag.ToHtmlString()));
        }
        public static ScopedHtmlHelper <T> VisibleWhenChecked <T>(this HtmlHelper <T> helper, Expression <Func <T, bool> > property, bool visible, Action <HtmlTag> mutator = null)
        {
            var propertyName = property.GetExpressionText();
            var name         = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(propertyName);

            var modelChecked = helper.ViewData.ModelState.ContainsKey(name);

            if (!modelChecked)
            {
                var modelValue = ModelMetadata.FromLambdaExpression(property, helper.ViewData).Model;
                modelChecked = modelValue != null && (bool)modelValue;
            }

            var container = new DivTag().NoClosingTag();

            if (visible && !modelChecked || !visible && modelChecked)
            {
                container.AddClasses("show-hide-hidden");
            }

            if (visible)
            {
                container.Attr("data-checkbox-checked-show", name);
            }
            else
            {
                container.Attr("data-checkbox-checked-hide", name);
            }

            if (mutator != null)
            {
                mutator(container);
            }

            helper.ViewContext.Writer.Write(container.ToHtmlString());

            return(new ScopedHtmlHelper <T>(helper, () =>
            {
                helper.ViewContext.Writer.Write($"</{container.TagName()}>");
            }));
        }
        public static ScopedHtmlHelper <T> VisibleWhenValue <T>(this HtmlHelper <T> helper, Expression <Func <T, bool?> > property, string value, Action <HtmlTag> mutator = null)
        {
            var propertyName = property.GetExpressionText();
            var name         = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(propertyName);

            string modelValue = null;
            var    modelState = helper.ViewData.ModelState;

            if (modelState.ContainsKey(name))
            {
                modelValue = (modelState[name].Value?.AttemptedValue ?? "").ToString();
            }
            else
            {
                modelValue = (ModelMetadata.FromLambdaExpression(property, helper.ViewData).Model ?? "").ToString();
            }

            var container = new DivTag().NoClosingTag();

            if (modelValue != value)
            {
                container.AddClasses("show-hide-hidden");
            }

            container.Attr("data-radio-show-name", name);
            container.Attr("data-radio-show-value", value);

            if (mutator != null)
            {
                mutator(container);
            }

            helper.ViewContext.Writer.Write(container.ToHtmlString());

            return(new ScopedHtmlHelper <T>(helper, () =>
            {
                helper.ViewContext.Writer.Write($"</{container.TagName()}>");
            }));
        }