asp.net mvc - Ajax Form Submit w/ preventDefault() always hits Success -


i've got .net mvc app. in 1 of views, there modal. inside modal, there form.

a few notes...

  • because form inside modal, i'm trying use little ajax prevent form doing default submission.
  • the form inside partial view
  • i'm using standard, built-in mvc validation scripts along data annotations in partial view's model. example:

    [display(name = "address")] [required()] [stringlength(200)] public string address { get; set; } 

this script i'm trying use prevent default submission:

        $(function () {             var frm = $('#form-accountprofileedit');             frm.submit(function (ev) {                 ev.preventdefault();                 $.ajax({                     type: frm.attr('method'),                     url: frm.attr('action'),                     data: frm.serialize(),                     success: function (data) {                         alert('ok');                     }                 });             });         } 

when submit button clicked , required fields empty, need validation messages displayed. but, instead, 'ok' dialog box pops up, indicating validation successful.

what doing wrong?

(i can post more code, if needs be.)

edit after:

here's form (slimmed down):

@using (html.beginform("accountprofileedit", "account", formmethod.post, new { id = "form-accountprofileedit", @class = "full-form" }))     {     @html.validationsummary(true)      @html.customtextboxfor(model => model.address)      <div style="text-align:right;">         <button type="submit" id="accountprofileedit-submit" name="accountprofileedit-submit" value="edit account" class="btn btn-primary" style="margin-left:5px;">edit account</button>         <button type="button" class="btn btn-primary" data-dismiss="modal">cancel</button>     </div> } 

the problem irrespective of whether modelstate valid controller going return 200(ok) status when return partial view post action. can override setting response status code.

if (!modelstate.isvalid) {     response.statuscode = 400;     return partialview("_formbody", model); } 

you need replace form body response server in order show validation messages.

$(function () {     var frm = $('#form-accountprofileedit');     frm.submit(function (ev) {         ev.preventdefault();         $.ajax({             type: frm.attr('method'),             url: frm.attr('action'),             data: frm.serialize(),             success: function (data) {                 alert('ok');             },             error: function (xhr, status, error) {             $('#form-accountprofileedit').html(xhr.responsetext);         }         });     }); } 

note: not replace whole of form element otherwise remove submit handler.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -