C
C#2y ago
teauxfu

❔ 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.
public class InputModel
{

[Required(AllowEmptyStrings = false)]
public string FirstName { get; set; }

[Required(AllowEmptyStrings = false)]
public string LastName { get; set; }

[Required]
[EmailAddress]
public string Email { get; set; }

[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
}
public class InputModel
{

[Required(AllowEmptyStrings = false)]
public string FirstName { get; set; }

[Required(AllowEmptyStrings = false)]
public string LastName { get; set; }

[Required]
[EmailAddress]
public string Email { get; set; }

[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
}
public async Task<IActionResult> OnPostAsync()
{
// only try adding user if attribute validation passes
if (ModelState.IsValid)
{
// ... try adding new user to db
if (result.Succeeded)
{
// ... other work
}
else
{
// ... check if failed due to existing user
if (duplicateAccountError)
ModelState.AddModelError(nameof(InputModel.Email), "Email already exists.");
}
}

return Page();
}
public async Task<IActionResult> OnPostAsync()
{
// only try adding user if attribute validation passes
if (ModelState.IsValid)
{
// ... try adding new user to db
if (result.Succeeded)
{
// ... other work
}
else
{
// ... check if failed due to existing user
if (duplicateAccountError)
ModelState.AddModelError(nameof(InputModel.Email), "Email already exists.");
}
}

return Page();
}
YouTube video proof of concept ? https://youtu.be/PtzH6vu91e8?t=666
Sameer 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
teauxfu
teauxfuOP2y ago
My PageModel has the following property
public class RegisterModel : PageModel
{
// ...
[BindProperty]
public InputModel Input { get; set; }
public class RegisterModel : PageModel
{
// ...
[BindProperty]
public InputModel Input { get; set; }
and the Razor page refers to it as
@page "/account/register"
@model BlazorUI.Areas.Identity.Pages.Account.RegisterModel
@page "/account/register"
@model BlazorUI.Areas.Identity.Pages.Account.RegisterModel
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 resolved
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server