Search code examples
asp.net-mvchtml-helper

htmlhelper dropdownlist selectlist to List<selectlist>


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 ?


Solution

  • 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")