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.
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
– Randrade
@Renilsonandrade I didn’t understand the difficulty. I edited the code so that your Controller receives the value of
Contrato
via Dropdown.– Leonel Sanches da Silva
You are using MVC or aspx?
– PauloHDSousa
My question is in the part after user selection. I won’t need some POST method?
– Randrade
I am using MVC
– Randrade
In this part, (public Actionresult Example(int Contract) { Httpcontext.Current.Session["Contract"] = Contract; ... }) is not receiving value, is null and void.
– Randrade
@Renilsonandrade Check in the generated HTML if Dropdown is named after
Contrato
, and whether thevalues
ofoptions
are whole. TheModelBinder
takes care of tying the variables to you since the<form>
is formatted correctly.– Leonel Sanches da Silva
I checked here, and I got it. Thank you Gypsy Morrison Mendez.
– Randrade
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?
– Randrade
@Renilsonandrade I edited the answer again.
– Leonel Sanches da Silva