Question:
I have this form, where user sees his account’s amount and can send money to other user just by providing email. The problem is, when user provides wrong data, function returns page, but I getNullExceptionError
.Actually, I think, that validators work fine, but there is something wrong with return type, or with how this function returns page.
Here is
Index.cshtml.cs
:Index.cshtml
. This is how it form looks like. When I provide wrong data, it show this NullExceptionError
in h5
.Answer:
When you POST you lose the state of your Model. You want to fill your Model when you POST.What I mean with that, is that you GET the page, it works fine. Then you POST some data, the POST code will run, but now it’s in a new instance of PageModel. This means that Model.account is null.
You can do one of two things:
- Fill the data before you do
return Page()
- Redirect to the GET action instead of returning Page().
Good luck!
If you have better answer, please add a comment about this, thank you!