Vishal Bhatt
Vishal Bhatt

Reputation: 11

Postback form after validation in MVC

I have one popup form which has a textbox that I use an AJAX post via jQuery to submit the form to the server. I use a strongly typed partial view for a popup, but when I keep my textbox blank and click the submit button, it displays a successful validation and posts form to the server.

Here is my ajax code :

$(document).ready(function () {

    //if($("#frmScenaria").valid()==true)

    //code to save detail
    $('#frmPost').submit(function (e) {
        var serializedForm = $('#frmPost').serialize();

            $.ajax({
                url: '@Url.Action("Save","Scenario")',
                type: "POST",
                dataType: "json",
                data: serializedForm,
                success: function (result) {
                    if (result.Success==true)
                    {
                        location.reload();
                    }
                },
                error: function(xhr, status, error) {

                    //alert(error.Message);
                }

            });

        return false;
    });
        //code to save detail end

});

Can any one help me to fix this issue?

Upvotes: 0

Views: 860

Answers (2)

Aravindan
Aravindan

Reputation: 855

Please use like below Note: Here i checked the from vqalidation before I post it.

$('#frmPost').click(function (e) {
    if($("#frmPost").validate())
    {
          var serializedForm = $('#frmPost').serialize();

            $.ajax({
                url: '@Url.Action("Save","Scenario")',
                type: "POST",
                dataType: "json",
                data: serializedForm,
                success: function (result) {
                    if (result.Success==true)
                    {
                        location.reload();
                    }
                },
                error: function(xhr, status, error) {

                    //alert(error.Message);
                }

            });

        return false;
    }
});

Upvotes: 0

BabyDuck
BabyDuck

Reputation: 1249

Use client-side validation instead. Like following: [Suppose textbox1 is the name of your textbox]

$('#frmPost').submit(function (e) {
        if($('#frmPost').textbox1.value == "") {
           alert('Please enter a value');
           $('#frmPost').textbox1.focus();
           return false;
        }
        var serializedForm = $('#frmPost').serialize();

            $.ajax({
                url: '@Url.Action("Save","Scenario")',
                type: "POST",
                dataType: "json",
                data: serializedForm,
                success: function (result) {
                    if (result.Success==true)
                    {
                        location.reload();
                    }
                },
                error: function(xhr, status, error) {

                    //alert(error.Message);
                }

            });

        return false;
    });

Hope this works for you, thank you.

Upvotes: 0

Related Questions