Populate Validationsummary with Data Annotations errors in Submit by AJAX

Asked

Viewed 94 times

0

I defined several Dataannotations for the model which the instancia form.

When the Form Ubmit is not by AJAX everything works as it should, ie the Validationsummary is filled with the ErrorMessage defined in my classes.

inserir a descrição da imagem aqui

The problem is when I perform the form Submit asynchronously with jquery.

Submit the form as follows:

$.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 even before form Submit? Such that the ValidationSummary is filled in with errors

And yes, I’m validating the model in Controller as well.

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 {

                    }

                }
            }).
};
  • As soon as I get home I’ll be in the code and I’ll let you know! (:

  • But in case it only checks whether it is valid or already displays the errors

  • He does both, checks and displays errors.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.