2
I’m 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 this in my View
:
@Html.DropDownListFor(model => model.setores, (IEnumerable<SelectListItem>)ViewBag.ID)
When I run the application on SelectList
appears the number of Sectors correctly, type have 3 sectors in my DB then appears 3 options, but in all is written Ramalaguia.Models.Sector
How can I fix this?
Actually the "setorNome" was already the name, and I had the "setorID" but I thought I should only put it if I wanted it to appear. But even so I understood your answer and it really worked. Thanks for the help. I just won’t raise your answer because I don’t have a reputation.
– Felipe Renan
the first is ID the second is the text I will edit the answer to get the correction fields
– Dorathoto