I have razor view with @Html.ValidationMessageFor helpers and jquery unobtrusive validation setup.
I want to call controller/action and to show eventual model state errors returned by action by using same validation logic that is already set in place.
I've made some code that does it but I was wondering if there is already way to do it automatically, i.e. if I capture HTTP Bad Request as AJAX response, I want to take out model state errors from response body and plug them in to unobtrusive validation.
I'm looking for complete recommended solution, not workarounds :)
Thanks!
You can return errors with
Json
result (How to get all Errors from asp.net mvc modelState?):Then manually show errors. Get form validator:
Then check that your fields are initialized correctly, for example you can look here (optional step):
OR
If everything is fine, then you could show error:
Where
Password
is field name andToo simple!
is error message.