How to send parameter to Actionresult?

Asked

Viewed 394 times

1

I have the ActionResult Login that validates my user and saves the data in a Session, if everything goes ok, redirects to the ActionResult BPAC:

[HttpPost]
public ActionResult Login(string pUsuario, string pSenha)
{
    oUsuario = modelOff.usuarios.SingleOrDefault(p => p.usuario1 == pUsuario && p.senha == pSenha);

    if (oUsuario == null)
    {
        return RedirectToAction("ErroLogin");
    }
    else
    {
        Session["usuario"] = oUsuario;
        return RedirectToAction("BPAC");
    }
}

To ActionResult BPAC needs a string calling for ibge to work, and I have to get the same Session established previously, but the system does not recognise it.

public ActionResult BPAC()
{
    if (Session["usuario"] == null)
    {
        return RedirectToAction("ErroLogin");
    }
    string ibge = Session["usuario"].ibge; //<<<<< ERRO AQUI
    List<Estabelecimento> listaEstabelecimento = new Estabelecimento().listaEstabelecimento(ibge);
    return View(listaEstabelecimento);
}

This line I marked is not recognized by the system. How do I use the Session here in my Controller?

1 answer

1


use Redirecttoaction by passing the parameters.

return RedirectToAction("BPAC", new { pUsuario = "admin", pSenha = "xpto" });

and put the parameters in the action

public ActionResult BPAC(string pUsuario, string pSenha)
  • I’ll need the data from Session in other Views, how to get through the Session?

  • use Viewbag. Ex. Viewbag.User and Viewbag.Password

Browser other questions tagged

You are not signed in. Login or sign up in order to post.