Call MVC 3 Client Side Validation Manually for ajax posts

后端 未结 8 591
长发绾君心
长发绾君心 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 22:52

    Try:

    //using the form as the jQuery selector (recommended)
    $('form').submit(function(evt) {
        evt.preventDefault();
        var $form = $(this);
        if($form.valid()) {
            //Ajax call here
        }
    });
    
    //using the click event on the submit button
    $('#buttonId').click(function(evt) {
        evt.preventDefault();
        var $form = $('form');
        if($form.valid()) {
            //Ajax call here
        }
    });
    

    This should work with jQuery ajax and MSAjax calls. Could also try using http://nuget.org/packages/TakeCommand.js or https://github.com/webadvanced/takeCommand it will automatically handle this for you.

提交回复
热议问题