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;
然后使用它
Html.DropDownList("OrderTypeList")
呈现
<select id="OrderTypeList" name="OrderTypeList"><option>System.Web.Mvc.SelectListItem</option> <option>System.Web.Mvc.SelectListItem</option> </select>
为什么它没有正确渲染选项?