MVC SelectList not working

asp.net-mvc

Solution

The constructor method you're calling when you do:

SelectList selectList = new SelectList(items);

Creates a set of SelectListItems that themselves point to SelectListItems (hence the weird option since it just calls ToString on the object). Instead set your list to the ViewData key directly

ViewData["OrderTypeList"] = items;

Problem

``` List<SelectListItem> items = new List<SelectListItem>(); if (a) { SelectListItem deliveryItem = new SelectListItem() { Selected = a.selected, Text = "Delivery", Value = "1" }; items.Add(deliveryItem); } if (b) { SelectListItem pickupItem = new SelectListItem() { Selected = b.selected, Text = "Pickup", Value = "2" }; items.Add(pickupItem); } SelectList selectList = new SelectList(items); ViewData["OrderTypeList"] = selectList; ``` Then using it with ``` Html.DropDownList("OrderTypeList") ``` Renders ``` <select id="OrderTypeList" name="OrderTypeList"><option>System.Web.Mvc.SelectListItem</option> <option>System.Web.Mvc.SelectListItem</option> </select> ``` Why it is not rendering options properly?

Original source