❔ Razor pages asp-validation-for="MyProperty " vs asp-validation-summary="All"
I am working on a simple registration form using a Razor page, and having trouble getting a message manually added with
ModelState.AddModelError
to show up.
My form has a <span asp-validation-for="Foo"></span>
for each input, as well as a <div asp-validation-summary="All"></div>
.
I've used attributes on my model to supply some validation rules. Those are working fine -- errors from failing the attributes will make an error message show up in both the span
and the div
The problem is that, after doing some custom logic (checking for existing user email) and manually adding an error to the ModelState
, the custom error message will only show up in the div
, not in the <span>
.
If I change the div to <div asp-validation-summary="ModelOnly"></div>
, then the manually added error stops showing there as well. This makes me suspect I'm adding the error wrong, but there's not a lot of room for variation. Any ideas on what I'm doing wrong? Thanks for taking the time to read/answer.
YouTube video proof of concept ? https://youtu.be/PtzH6vu91e8?t=666Sameer Saini
YouTube
Form Validations In ASP.NET Core Razor Pages - Model Validations In...
👉ASP.NET Razor Pages Full Course:
https://www.udemy.com/course/aspnet-core-razor-pages-web-application-development/?couponCode=MAY2023
In this video, we will see how we can create form and model validations in an ASP.NET Core 6 Razor Pages application.
We will submit a form in a razor pages application and then validate the values of the form a...
2 Replies
My
PageModel
has the following property
and the Razor page refers to it as
strangely, using the @Html.ValidationMessage
directive works... just not the <span asp-validation-for>
. weird
hmm, seems to be a nesting issue. by moving getting rid of the separate InputModel property and hoisting the props within up to the PageModel, and binding them there directly, the issue seems to be resolvedLooks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.