Fill ValidationSummary with Data Annotations Errors in Submit by AJAX

0

Define several DataAnnotations for the template which the instance instance.

When the form submission is not for AJAX, everything works as it should, that is, ValidationSummary is populated with ErrorMessage defined in my classes.

TheproblemiswhenIsubmittheformasynchronouslywithjquery.

Isubmittheform'sSubmitasfollows:

$.ajax({url:'@Url.Action("CriarEvento")',
                    type: 'POST',
                    data: $(this).serialize() + '?preco=' + preco,                        
                    success: function (obj) {                            
                        if (obj.success == true) {
                            alert('evento salvo com sucesso');
                            $('#fecharCrEvento').click();
                            $('#calendario').fullCalendar('refetchEvents');
                        }
                        else {

                        }

                    }
                }).

How can I use the same validation defined in DataAnnotations before even submitting form? In such a way that ValidationSummary is filled with errors

And yes, I'm validating the model on the Controller as well.

    
asked by anonymous 20.07.2015 / 03:59

1 answer

1

To verify this, use the code below:

if ($("#idForm").valid())
{
           $.ajax({
                url: '@Url.Action("CriarEvento")',
                type: 'POST',
                data: $(this).serialize() + '?preco=' + preco,                        
                success: function (obj) {                            
                    if (obj.success == true) {
                        alert('evento salvo com sucesso');
                        $('#fecharCrEvento').click();
                        $('#calendario').fullCalendar('refetchEvents');
                    }
                    else {

                    }

                }
            }).
};
    
20.07.2015 / 20:17