I'm trying to populate a @Html.DropDownList with a List using MVC and Razor. Bellow is my code for the controller. So I need to pick the list out of my ViewBag and fill the dropdownlist.
public ActionResult Register()
{
sparklingEntities context = new sparklingEntities();
var query = (from discs in context.Disciplines
select discs).ToList();
List<string> listOfDiscs = new List<string>();
foreach (var item in query)
{
listOfDiscs.Add(item.Discipline);
}
ViewBag.ListOfDisciplines = listOfDiscs;
return View();
}
Thanks for the help.
If this is in an Editor for your model property:
@Html.DropDownList("", new SelectList(ViewBag.ListOfDisciplines, Model))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With