Validate response on controller is giving error

1

I need to validate the answer in my method in the controller. For this I need an if. Why does the method not consider return within an IF? So:

public JsonResult ValidaLogin(string _email, string _senha)
        {
            INETGLOBALEntities db = new INETGLOBALEntities();
            bool validalogin = false;

            EncriptyDecripty cripto = new EncriptyDecripty();

            string s = cripto.Encrypt(_senha);

              var  result_login = (from login in db.tbl_usuario
                                    where login.email == _email && login.senha_usuario == _senha
                                    select new { login.email, login.nm_usuario }).ToList();

             if(result_login.Count > 0)
                return Json(new { result_login }, JsonRequestBehavior.AllowGet);
        }

It gives error, saying that the method does not have a return. How do I validate it?

My Ajax

function ValidaLogin() {

    $.ajax({
        url: '/Login/ValidaLogin',
        datatype: 'json',
        contentType: "application/json; charset=utf-8",
        type: "POST",
        data: JSON.stringify({ _email: $('#inputEmail').val(), _senha: $('#inputPassword').val() }),
        success: function (data) {

            $(window.document.location).attr('href', '/Pesquisa/Pesquisa');
        },
        error: function (error) {

            alert('Usuário ou senha digitados de forma incorreta.');
        }
    });

}
    
asked by anonymous 05.08.2014 / 15:56

1 answer

2

It gives an error because you do not actually have a return if the if condition does not exist. A function of type JsonResult is always waiting for a return type. Do:

if(result_login.Count > 0){
    return Json(new { result_login }, JsonRequestBehavior.AllowGet);
}else{
    return Json(JsonRequestBehavior.AllowGet);
}

Then in javascript, you check if the result_login variable exists.

EDIT:

In your case you do:

function ValidaLogin() {    
    $.ajax({
        url: '/Login/ValidaLogin',
        datatype: 'json',
        contentType: "application/json; charset=utf-8",
        type: "POST",
        data: JSON.stringify({ _email: $('#inputEmail').val(), _senha: $('#inputPassword').val() }),
        success: function (data) {
            if(data != null){//Dá um alert do data para ver o que retorna. Possivelmente até pode dar undefined
                $(window.document.location).attr('href', '/Pesquisa/Pesquisa');
            }else{
                alert('Usuário ou senha digitados de forma incorreta.')
            }
        }
    });
}
    
05.08.2014 / 16:03