jquery - $('#MyForm').valid() always returning false -
jquery - $('#MyForm').valid() always returning false -
i trying post form using ajax post in mvc razor. , trying validate form before posting database. though form valid, blocking form post.
$(function () { $('#myform').submit(function () { if ($('#myform').valid()) { //$("#divloading").show(); $.ajax({ type: "post", url: '@url.action("something", "something")', data: $('#myform').serialize(), datatype: 'json', error: function (xhr) { $("#message").text(xhr.statustext); $('#divloading').hide('fast'); }, success: function (result) { //$('#divloading').hide('fast'); if (result.issuccess) { $("#message").html(result.message); $('#myform')[0].reset(); } } }); } }); });
begin form contains
@using (ajax.beginform("something", "something", new ajaxoptions { insertionmode = insertionmode.replace, httpmethod = "post" }, new { @id = "myform" })) { }
thanks in advance
your problem here still utilize form default action, page reload before getting ajax response. seek this:
$('#myform').submit(function (event) { event.preventdefault(); if ($('#myform').valid()) { //$("#divloading").show(); $.ajax({ type: "post", url: '@url.action("something", "something")', data: $('#myform').serialize(), datatype: 'json', error: function (xhr) { $("#message").text(xhr.statustext); $('#divloading').hide('fast'); }, success: function (result) { //$('#divloading').hide('fast'); if (result.issuccess) { $("#message").html(result.message); $('#myform')[0].reset(); } } }); }
where difference:
$('#myform').submit(function (event) { event.preventdefault();
jquery asp.net-mvc-5 razor-2 jquery-post
Comments
Post a Comment