I would like to insert in the DropDownList
only the sectors that have already been used in the contact register.
In my ContatosController
no ActionResult
in method Index
I did this, but the problem that is happening is that you are only inserting the last one, so I will have to insert it into a new list and then insert this list in the DropDownList
.
I would like to know the right way
//AQUI ESTOU SEPARANDO EM GRUPOS OS SETORES JÁ UTILIZADOS EM CONTATOS
var setoresEmCadastro = db.Contatos.GroupBy(s => s.SetorId).ToList();
//AQUI PEGO O CÓDIGO DE setoresEmCadastro E PESQUISO
foreach (var item in setoresEmCadastro)
{
var s = db.Setores.Where(a => a.SetorId == item.Key);
//INSIRO NO COMBOBOX
ViewBag.SetorID = new SelectList(s, "SetorId", "Nome");
}