Use Session as Query Parameter

Asked

Viewed 279 times

0

Hello, I have a method to login the system, soon after I need the user to select an option in a dropdown and save this value in a Session. Now I need to put the stored value in Session, inside a LINQ query. I’ll post the codes, which you’ll understand better.

Method to store the die 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 place the value stored by Session:

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

        return View(usuarios);
    }

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; }
    }
}

Use:

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);
}
  • When creating Helper, I get the following error: "Portalrh.WebUI.Models.SessionHelper.Contract: cannot declare instance Members in a Static class", you know what could be?

  • @Renilsonandrade My own mistake. I edited the answer.

  • In the Get{Return Httpcontext.Current.Session["Contract"];} part I get the following error: "cannot Convert Expression type 'Object' to Return type int"

  • @Renilsonandrade I edited again

  • Between "(int)" and "Return" I get "Expression Expected"( but does not say which). And the rest I continue with the same error. "cannot Convert Expression type 'Object' to Return type int"

  • I made a convertTo, and it worked. get ' Return Convert.Toint16(Httpcontext.Current.Session["Contract"]); }

  • Thanks Gypsy Morrison Mendez, helped me a lot.

Show 2 more comments

Browser other questions tagged

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