Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate @Html.DropDownList with a List<string> using MVC

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.

like image 522
wardh Avatar asked Jun 13 '11 16:06

wardh


1 Answers

If this is in an Editor for your model property:

@Html.DropDownList("", new SelectList(ViewBag.ListOfDisciplines, Model))
like image 176
Ethan Cabiac Avatar answered Sep 22 '22 12:09

Ethan Cabiac