检查验证并阻止在@ Ajax.BeginForm中将表单提交到服务器

时间:2014-11-26 08:32:20

标签: jquery ajax json asp.net-mvc-4 unobtrusive-javascript

我在index.cshtml中有代码

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

和_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**
               }             
       }

   }

这里我有两个案例来自ActionEvent Method Case OK和Case ERROR它将结果返回为Json

  1. 如果Controller返回Case OK我想将表单中输入的详细信息保存到数据库中
  2. 如果Controller返回Case ERROR,我不想将表单中输入的详细信息保存到数据库中
  3. 但是在这两种情况下数据都保存到数据库中..但验证已激活消息显示正确.. 我能为此做些什么..请指导我..

0 个答案:

没有答案