Reputation: 5002
I create a select list and set it to viewbag then shot it as dropdownlist in view.. but it throws this exception:
{"Cannot convert type 'System.Collections.Generic.List<System.Web.Mvc.SelectListItem>' to 'System.Web.Mvc.SelectList'"}
here is my controller and html helper in view:
ViewBag.ProjeTipiList = valueSettingsService.GetRuzgarTurbunProjeTipiList().OrderBy(t => t.ProjeTipKod).ToSelectList(t => t.ProjeTipKod, t => t.Id.ToString(), "...Seçiniz...");
@Html.DropDownListFor(model => model.ProjeTipId, (SelectList)ViewBag.ProjeTipiList, "Select One")
how can I fix this ?
Upvotes: 1
Views: 1822
Reputation: 7866
Because DropDownList does not accept a list of strings, It accepts IEnumerable<SelectListItem>.
@Html.DropDownListFor(model => model.ProjeTipId, ((List<string>)ViewBag.ProjeTipiList).Select(m => new SelectListItem { Text = m, Value = m }))
Upvotes: 1
Reputation: 63
I would do it this way:
ViewBag.ProjeTipiList = valueSettingsService.GetRuzgarTurbunProjeTipiList().OrderBy(t => t.ProjeTipKod).ToList();
@Html.DropDownListFor(model => model.ProjeTipId, new SelectList(ViewBag.ProjeTipiList,"ProjeTipKod","Id", "...Seçiniz...") ,"Select One")
Upvotes: 3