Redirect to Action according to if

1

I have a Controller :

[HttpPost]
public ActionResult Login(string pUsuario, string pSenha)
{
     usuario oUsuario = modelOff.usuarios.Where(p => p.usuario1 == pUsuario && p.senha == pSenha).SingleOrDefault();
     if (oUsuario == null)
         return IR_PARA_ACTION_1;
     else
         return IR_PARA_ACTION_2;
}

How do I select Action according to the result of my if ?

    
asked by anonymous 28.07.2017 / 21:18

1 answer

3

Using RedirectToAction , passing the action name as a parameter.

Tip: The SingleOrDefault method receives a predicate, you do not need to Where and then SingleOrDefault . Also, else is unnecessary.

[HttpPost]
public ActionResult Login(string pUsuario, string pSenha)
{
     usuario oUsuario = modelOff.usuarios.SingleOrDefault(p => 
                                           p.usuario1 == pUsuario && p.senha == pSenha);
     // ^^ Dica 1: Usar apenas SingleOrDefault()

     if (oUsuario == null)
         return RedirectToAction("Action1");

     return RedirectToAction("Action2"); // <- Dica 2: Não precisa do else
}
    
28.07.2017 / 21:19