In my entity model I have a required attribute:
[Required]
public string Occupation { get; set; }
I have an OnPostAsync
Method that returns the page if the user submits invalid information
if (!ModelState.IsValid)
{
return Page();
}
If disable javascript (run server side validation) and submit a form with a blank Occupation field, I get the following error:
An unhandled exception has occurred while executing the request.
System.NullReferenceException: Object reference not set to an instance of an object.
If I instead return RedirectToPage()
, everything is fine, but I don't get the error messages displayed on the client.
The official microsoft docs simply return Page()
. Why I am I getting unhandled exceptions, and how can I preserve validation errors on the client while avoiding them?