Reputation: 131
If I would need to display errors related to form fields, I would use the tag helpers in View:
<span asp-validation-for="SomeField"></span>
and either use data validation attributes in the model class or call
ModelState.AddModelError("SomeField", "Error message");
in controller.
Now I need to display an error message in the Login page if login fails. Something general like "Invalid login attempt." Is there an inbuilt mechanism for that?
Adding a new unused property in a model so I can use "asp-validation-for" tag helper and AddModelError feels wrong. Should I use ViewBag or ViewData or is another way?
Upvotes: 0
Views: 201
Reputation: 131
Just found how to do it.
Add a new div using asp-validation-summary tag helper in the view.
<div asp-validation-summary="ModelOnly" class="text-warning"></div>
And in controller action generate an error using an empty string:
ModelState.AddModelError(string.Empty, "Invalid login attempt");
Upvotes: 1