HtmlTags/htmltags

How to append validation message ASP.NET Core to div

Closed this issue · 1 comments

Hi,

How do I create a HTML tag to display a "form-group" (Bootstrap4) including the validation message? The tag helper is <span asp-validation-for="Property" class="text-danger"></span>.

What I've got is

public static HtmlTag FormBlock<T>(this IHtmlHelper<T> helper,
            Expression<Func<T, object>> expression,
            Action<HtmlTag> labelModifier = null,
            Action<HtmlTag> inputModifier = null
        ) where T : class
        {
            labelModifier = labelModifier ?? (_ => { });
            inputModifier = inputModifier ?? (_ => { });

            var divTag = new HtmlTag("div");
            divTag.AddClass("form-group");

            var labelTag = helper.Label(expression);
            labelModifier(labelTag);

            var inputTag = helper.Input(expression);
            inputModifier(inputTag);

            var htmlAttributes = new Dictionary<string, string>
            {
                ["class"] = "text-danger"
            };
            // TODO Create HtmlTag so it can be modified like input and label 
          

            divTag.Append(labelTag);
            divTag.Append(inputTag);
  
            return divTag;
        }

Thanks

You'll need to poke into the model state to pluck out the validation messages. You can find out how this is done by going through the ASP.NET Core source code and finding that tag helper that's built in.