Search code examples
asp.net-mvc-5

Populate Select using ViewBag without using html Helper


I Have this in my controller

ViewBag.list = new SelectList(db.ListOfDB, "Value", "Text");

I need to create a Select, but without using the @Html.DropDownList I try to use this:

<select id="mylist"> ??? (selectitem) @ViewBag.list ??? </select>

How create select based from Viewbag, but not use Html.Helper


Solution

  • You can pass your list to the view on your model. You could then do

    <select id="myList">
    @foreach(var item in Model.myList)
    {
       <option value="@item.Value">item.Name</option>
    }
    </select>