Insert only items in a Dropdownlist that have already been used in the main register?

Asked

Viewed 33 times

0

I would like to insert in the DropDownList only the sectors that have already been used in contact registration.

In my ContatosController in the ActionResult in the method Index I did this, but the problem that’s happening is that I’m only inserting the last one, so I’ll have to insert it into a new list and then insert this list into the DropDownList.

I wish I knew 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");
}

1 answer

0

Resolved the way below.

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");

Browser other questions tagged

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