Insert only items in a DropDownList that have already been used in the master data?

0

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");
}
    
asked by anonymous 29.01.2017 / 14:14

1 answer

0

Solved as follows.

But I leave it open. to those who have a better solution.

var setoresEmCadastro = db.Contatos.GroupBy(s => s.SetorId).ToList();

   var sd = new List<Setor>();

        foreach (var item in setoresEmCadastro)
        {
            var s = db.Setores.Where(a => a.SetorId == item.Key).ToList() ;

            var fdp = new Setor
            {
                SetorId = s[0].SetorId,
                Nome = s[0].Nome
            };

            sd.Add(fdp);
        }

        ViewBag.SetorID = new SelectList(sd, "SetorId", "Nome");
    
29.01.2017 / 17:55