Ajax.BeginForm onFailure处被调用时的ModelState是无效的 [英] Ajax.BeginForm OnFailure invoked when ModelState is InValid

查看:280
本文介绍了Ajax.BeginForm onFailure处被调用时的ModelState是无效的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要称之为onFailure处时的ModelState是不是在控制有效。

I want to call "OnFailure" when ModelState is not valid in controller.

在我的LoginView

In My LoginView

 @using (Ajax.BeginForm("Login", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "Login",InsertionMode = InsertionMode.Replace, OnSuccess = "Success", OnFailure = "onError" }))
 {

 } 

在控制器

[httpPost]
public ViewResult Login(LoginModel model)
{
   if (ModelState.IsValid)
   {

   }
   else
   { 
     ModelState.AddModelError("login is fail")
   }
   return View("Login",model)
}

所以我要调用的onSuccess方法,如果ModelState中是有效的,如果它失败了,只调用onError方法与显示其在模型状态的所有错误。

so i want call onSuccess Method if ModelState is valid and if it fail then call only OnError method with display all error which are in Model State.

推荐答案

下面是你可以做什么:

[HttpPost]
public ActionResult Login(LoginModel model)
{
    if (ModelState.IsValid)
    {
        // everything went fine and we want to redirect in this case =>
        // we pass the url we want to redirect to as a JSON object:
        return Json(new { redirectTo = Url.Action("SomeController", "SomeAction") });
    }
    else
    { 
        // there was an error => add an error message
        ModelState.AddModelError("login is fail")
    }

    // return a partial view instead of a full vire
    return PartialView("Login",model)
}

和那么所有你需要的是成功的功能:

and then all you need is the Success function:

@using (Ajax.BeginForm("Login", new AjaxOptions { HttpMethod = "POST", OnSuccess = "loginAjaxSuccess" }))
{

} 

在这你可以在你这种情况下,测试:

in which you could test in which case you are:

function loginAjaxSuccess(result) {
    if (result.redirectTo) {
        // the controller action returned a JSON result => it was a successful login
        // => we redirect the browser to this url
        window.location.href = result.redirectTo;
    } else {
        // the action returned a partial view with the form containing the errors
        // => we need to update the DOM:
        $('#Login').html(result);
    }
}

通过,如果你使用的是不显眼的客户端验证错误的情况下,你在哪里刷新,你需要手动强制新的验证规则解析的形式,否则,下一次你试图提交表单的方式,客户端验证不会工作:

By the way if you are using unobtrusive client side validation in the case of error where you are refreshing the form you will need to manually force the parsing of the new validation rules, otherwise next time you attempt to submit the form, client validation won't work:

} else {
    // the action returned a partial view with the form containing the errors
    // => we need to update the DOM
    $('#Login').html(result);

    // Now that the DOM is updated let's refresh the unobtrusive validation rules on the form:
    $('form').removeData('validator');
    $('form').removeData('unobtrusiveValidation');
    $.validator.unobtrusive.parse('form');
}

这篇关于Ajax.BeginForm onFailure处被调用时的ModelState是无效的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆