Call MVC 3 Client Side Validation Manually for ajax posts

后端 未结 8 609
长发绾君心
长发绾君心 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:06

    .Valid() works. i.e it tells you whether your form is valid. However alone it does not help to show AND hide messages correctly. here's my manual validation method

    function validate()
            {
                //valid() not only tells us whether the form is valid but 
                //also ensures that errors are shown !!!
                if ($("form").valid())
                {
                    //if the form is valid we may need to hide previously displayed messages
                    $(".validation-summary-errors").css("display", "none");
                    $(".input-validation-error").removeClass("input-validation-error");
                    return true;
                }
                else
                {
                    //the form is not valide and because we are doing this all manually we also have to
                    //show the validation summary manually 
                    $(".validation-summary-errors").css("display", "block");
                    return false;
                }
            }
    

提交回复
热议问题