开发者

How to use MicrosoftMvcValidation with jQuery.Ajax in ASP.net MVC2?

开发者 https://www.devze.com 2023-01-13 01:06 出处:网络
using asp.net mvc2, Data Annotations, MicrosoftAjax.js,MicrosoftMvcValidation.js, jquery for ajax I have contact form and i am using Data Annotations for the ContactFormModel.

using asp.net mvc2, Data Annotations, MicrosoftAjax.js,MicrosoftMvcValidation.js, jquery for ajax

I have contact form and i am using Data Annotations for the ContactFormModel.

I add this line <% Html.EnableClientValidation(); %> to top of the form.

When i click the submit button client validation works perfectly. Now i have changed my mind and want to post the form with jQuery.Ajax.

This time i want to accomplish this.

  1. Click submit button.
  2. MicrosoftMVCValidation does the client validation and renders the errors on the clientside.
  3. If Model is valid i meant if the validation passed i want my jQuery ajax to get involved.

But when i clicked the submit button both ajax post 开发者_运维问答and mvc client validation works. How can i get the things in right order.

1.Mvc Client validation

2. Then jQuery.Ajax Post.


            var myForm = $("#MainForm");
            var formContext = myForm[0]['__MVC_FormValidation'];
            var errors;
            if (formContext) {
                // validate the form
                errors = formContext.validate("submit");
            }
            if (!formContext || errors.length == 0) {
                // no errors so submit to server
                ...
            }
0

精彩评论

暂无评论...
验证码 换一张
取 消