Check validation and pervent form submitting into server in @Ajax.BeginForm

198 views Asked by At

I have a code in index.cshtml

 @using (@Ajax.BeginForm("ActionEvent", "Fielding", new AjaxOptions { OnSuccess = "onBDSuccessFunction", HttpMethod = "post" }, new { id = "frmField" }))
    { 
        @Html.Partial("../Fielding/_CreateOrEdit", Model)
    }

and in _CreateOrEdit

function onBDSuccessFunction(result, status, xhr) {
       var errorMessage = '';
       switch (String(result.Result).toUpperCase()) **2 cases from controller when Actionevent Method fired** {
        case "OK":
               var _msg = "Data Saved Successfully";
               alert(_msg);
               return true; **saved data into database only happens here**
               break;
           case "ERROR":
               if(result.Message=='1')
               {
               alert(result.Messag);
               return false; **prevent submiting form here**
               }             
       }

   }

Here i have 2 cases from ActionEvent Method Case OK and Case ERROR it returns result as Json

  1. If Controller returns Case OK I want to save the entered details in the form into database
  2. If Controller returns Case ERROR I dont want to save the entered details in the form into database

But in both cases the data saved into database .. but the validation fired message shows correctly.. What can i do for this.. please guide me..

0

There are 0 answers