Ajax:调用一个布尔动作

本文关键字:一个 布尔动 调用 Ajax | 更新日期: 2023-09-27 18:14:01

我想从Action中获得一个布尔值,并根据相应AJAX函数中的返回值进行测试。

我在Action上设置了一个断点,但是我的提交没有调用Action就通过了。

这是我的动作:

[HttpGet]
[AllowAnonymous]
public Boolean VerifyEmailExist(string email)
{
    if(db.UtilisateurSet.Where( p => p.Utilisateur_EmailPrinc == email).ToList().Count() != 0)
    {
        return false;
    }
    else
    {
        return true;
    }
}
这是我的AJAX方法:
function VerifyEmailExist(champ) {
    $.ajax({
        url: "/Utilisateur/VerifyEmailExist",
        type: 'Get',
        data: {
            email: champ,
        },
        success: function (response) {
            if (response) {
                champ.style.backgroundColor = "#fba";
                alert("Votre Email Existe dèja!");
                return false;
            } else {
                champ.style.backgroundColor = "";
                return true;
            }
        },
        error: function () {
            alert("something seems wrong");
        }
    });
}

Ajax:调用一个布尔动作

不能返回布尔值结果,返回类型必须继承ActionResult。你可以返回JsonResult:

[HttpGet]
[AllowAnonymous]
public ActionResult VerifyEmailExist(string email)
{
    if(db.UtilisateurSet.Where( p => p.Utilisateur_EmailPrinc == email).ToList().Count() != 0)
    {
        return Json(new { status = false });
    }
    else
    {
        return Json(new { status = true });
    }
}

和ajax成功事件:

success: function (response) {
    if (response.status) {
       champ.style.backgroundColor = "#fba";
       alert("Votre Email Existe dèja!");
       return false;
    } else {
       champ.style.backgroundColor = "";
       return true;
    }
 },