Save data to a "Global Viewable"

Asked

Viewed 688 times

3

Hello, I have a Dropdownlist, coming from a Viewbag, and I need to write this value in some kind of "global variable". I researched and saw that the best ways to do this, is to record in a Viewbag or in a Viewmodel.

Scenario: When the user logs into the system, he will have to choose which contract he wants to access( in a Dropdownlist). I need that value to be saved, and that you can use it in a query right after, to show the data only of this contract. The login is working correctly, and I managed to list the contracts in a Dropdownlist, coming from a viewbag. I just need to take this selected value and use in another query.

Dropdownlist code:

            ViewBag.Contrato = usuarioRepository.Lista.Where(u => u.sLogin == autenticacaoProvider.UsuarioAutenticado.Login).Select(u => u.SqContrato);

View calling the Dropdownlist:

        @Html.DropDownList("Contrato", new SelectList(ViewBag.Contrato, "Contrato"))

I’m having trouble, creating the method in the controller that will save this Falor, so I can use it again.

If you need more code, just comment, that put here.

2 answers

2

Do an assignment to Session in your Controller:

public ActionResult Exemplo(int Contrato) {
    HttpContext.Current.Session["Contrato"] = Contrato;

    ...
}

Reclaiming the value of:

public ActionResult OutroExemplo() {
     var Contrato = (int)HttpContext.Current.Session["Contrato"];

    ...
}

This, however, is not the best approach for classic load balancing problems. The best approach involves implement your own session manager, but this is more complicated. Start by making the first approach that is simpler.

  • Until this part, I had already tried too. The problem is that the value will come from a Dropdownlist, which the user will choose. And I’m having a hard time setting them up for Session

  • @Renilsonandrade I didn’t understand the difficulty. I edited the code so that your Controller receives the value of Contrato via Dropdown.

  • You are using MVC or aspx?

  • My question is in the part after user selection. I won’t need some POST method?

  • I am using MVC

  • In this part, (public Actionresult Example(int Contract) { Httpcontext.Current.Session["Contract"] = Contract; ... }) is not receiving value, is null and void.

  • @Renilsonandrade Check in the generated HTML if Dropdown is named after Contrato, and whether the values of options are whole. The ModelBinder takes care of tying the variables to you since the <form> is formatted correctly.

  • I checked here, and I got it. Thank you Gypsy Morrison Mendez.

  • Now I just have one more problem. Session I’m only able to recover the value in this view, and I need it to be available for the entire controller. How do I do that?

  • @Renilsonandrade I edited the answer again.

Show 5 more comments

2


In the post of your form (may be GET yes), you can treat the code in your controller as follows:

public class ContaController : Controller
{
  [HttpPost]
  public ActionResult SelecionarContrato(int Contrato)
  {
        Session["contrato"] = model.Contrato;
        //Some code here...
        return RedirectToAction("Index");
  }

 public ActionResult VerificarContrato()
 {
      var contrato = (int)Session["contrato"];

 }
}

The method VerificarContrato is an example of information retrieval. After placing it on Session, check that the object is null and do the proper processing. In the example is an integer, but you can store complex objects as well.

  • In this method I am getting a "'System.Nullreferenceexception' ". I put a writeline to check, and it is returning as null, when arriving at "var contract = (int)Session["contract"];", always receives 0, and I get this error

Browser other questions tagged

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