public IActionResult OnPost() {
if (ModelState.IsValid) {
\\ save it
}
return Page();
}
In my razor code, I've got the above checking if the model is valid. This works fine, if the model is valid, it saves, if not it goes to return Page();
which is what all the documentation I can find says to do.
The problem is that if the model is invalid, the return Page();
reloads the page so I've lost all the entered values and none of the validation messages are loaded from the model.
What should I use instead of return Page();
to display the validation messages and keep the invalid information ?
------------------- Edit with ViewModel
So with this as my OnPost
[BindProperty]
public theVM.ModelinVM theVMInstance { get; set; }
public IActionResult OnPost() {
if (ModelState.IsValid) {
\\ save it
}
return theVMInstance;
}
and this is the view model
public class theVM {
public IQueryable<originalModel> ModelinVM { get; set; }
}
return theVMInstance;
now says Cannot implicitly convert type 'theVM.ModelinVM' to 'Microsoft.AspNetCore.Mvc.IActionResult'.
It's expecting an IActionResult
so doesn't it have to be compatible with that?
It didn't work because I hadn't put
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
at the top of the .HTML page.
I didn't need the OnPost() method, the jQuery did th ejob for me.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments