Use Session as Parameter of a query

0

Hello, I have a method to log in to the system, right after I need the user to select an option in a dropdown and save that value in a session. Now I need to put the value stored in the session, inside a LINQ query. I'll post the codes, which will understand better.

Method to store the data in a session:

 public ActionResult Contrato()
    {      
        var contrato = new Usuario() { SqContrato = 0 };

        return View(contrato);
    }

    [HttpPost]
    public ActionResult Contrato(Int16? Contrato)
    {

        System.Web.HttpContext.Current.Session["Contrato"] = Contrato;

        return View();
    }

Method where I need to put the value stored by the session:

    public ViewResult Ferias()
    {
        var usuarios =
            funcionarioFeriasRepository.Lista.Where(r => r.slogin == autenticacaoProvider.UsuarioAutenticado.Login && r.SqContrato == "SESSION AQUI"
                .ToList();

        return View(usuarios);
    }
    
asked by anonymous 11.01.2015 / 23:29

1 answer

1

Make a Helper:

public static class SessionHelper
{
    public static int Contrato {
        get { return (int)HttpContext.Current.Session['Contrato']; }
        set { HttpContext.Current.Session['Contrato'] = value; }
    }
}

Usage:

public ViewResult Ferias()
{
    var contrato = SessionHelper.Contrato;
    var usuarios =
        funcionarioFeriasRepository.Lista.Where(r => r.slogin == autenticacaoProvider.UsuarioAutenticado.Login && r.SqContrato == contrato)
            .ToList();

    return View(usuarios);
}
    
11.01.2015 / 23:36