SelectList with ViewBag

1

I am trying to make a SelectList using ViewBag . But when I run my code it shows where the information I'm trying to access is, for example:

NomeProjeto.Models.NomeModel

I'm using the following code in my Controller :

var setores = new List<Setor>();

using (RamaDb db = new RamaDb())
{
     setores = db.Setores.ToList();
}

ViewBag.ID = new SelectList(setores, "setorNome");

And the one in my View :

@Html.DropDownListFor(model => model.setores, (IEnumerable<SelectListItem>)ViewBag.ID)

When I run the application on SelectList , the number of Sectors appears correctly, I have 3 sectors in my DB so 3 options appear, but in all it is written RamalAguia.Models.Setor

How can I fix this?

    
asked by anonymous 31.05.2016 / 21:01

1 answer

1

ViewBag.ID = new SelectList(setores, "setorNome");

for

ViewBag.ID = new SelectList(setores, "setorID","setorNome");

If you want to send which will be selected is the 4th field, eg:

 ViewBag.ID = new SelectList(setores, "setorID","setorNome", 4);

where 4 would be the sectorID selected

    
31.05.2016 / 21:32