Hawke
Hawke

Reputation: 584

Validating a form server side - ASP.NET Core 2.2 Razor Pages

I have a form which I have been validating with the standard [Required] helper tags.

I needed to add validation to see if the entry was already in AD, so I used:

public async Task<IActionResult> OnPostAsync(){

 if (!ModelState.IsValid)
     {

        return Page();
     }

if (MyADClass.Exists(Model.id)){

    Error = "An account already exists for" + Model.id;

    return RedirectToPage(new { message = Error });
}

//Else do stuff

}

The problem with this approach, is that it refreshes the whole page and loses the values currently in the form. What is the easiest way to preform the validation after the user has clicked submit, but without losing the model data?

Upvotes: 1

Views: 428

Answers (1)

Hawke
Hawke

Reputation: 584

Realised I could just set the Message directly as part of the model and call return Page();

Thanks @ADyson

Upvotes: 2

Related Questions