Displaying model state errors after ajax call on Razor views

前端 未结 1 964
既然无缘
既然无缘 2021-02-02 17:39

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 erro

相关标签:
1条回答
  • 2021-02-02 18:16

    You can return errors with Json result (How to get all Errors from asp.net mvc modelState?):

    var allErrors = ModelState.Values.SelectMany(v => v.Errors);
    

    Then manually show errors. Get form validator:

    var validator = $("form").validate();
    

    Then check that your fields are initialized correctly, for example you can look here (optional step):

    validator.settings.rules
    

    OR

    validator.settings.messages
    

    If everything is fine, then you could show error:

    validator.showErrors({"Password": "Too simple!"});
    

    Where Password is field name and Too simple! is error message.

    0 讨论(0)
提交回复
热议问题