Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to append validation message ASP.NET Core to div #92

Closed
joacar opened this issue Mar 20, 2018 · 1 comment
Closed

How to append validation message ASP.NET Core to div #92

joacar opened this issue Mar 20, 2018 · 1 comment

Comments

@joacar
Copy link

joacar commented Mar 20, 2018

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

@jbogard
Copy link
Contributor

jbogard commented Mar 21, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants