Call MVC 3 Client Side Validation Manually for ajax posts

后端 未结 8 583
长发绾君心
长发绾君心 2020-11-30 22:16

I am creating an MVC 3 web application. I want to use Data Annotations on my entity class and then use unobtrusive client side validation before making a post back to the se

8条回答
  •  渐次进展
    2020-11-30 23:03

    IMPORTANT!!:

    Paul's solution is the correct answer to the question, not Dr Rob's.

    Although you can just use valid() instead of validate().form().

    But more importantly, there really is no reason to restrict your code as suggested by Dr Rob, ie, not .click and only use .submit. That isn't what solved the problem! What solved the problem was wrapping the $.ajax(...) call in the if statement. Ie:

    if($("#MyForm").valid())
    {
        //call to $.ajax or equivalent goes in here.
    }
    

    I think that needs clarifying as otherwise the real answer to the problem is obfuscated.

提交回复
热议问题