我创建一个选择列表并将其设置为viewbag然后在视图中将其作为下拉列表拍摄..但它会引发此异常:
{“无法转换类型 'System.Collections.Generic.List< System.Web.Mvc.SelectListItem>'至 'System.Web.Mvc.SelectList'“}
这是我的控制器和html帮助器在视图中:
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")
我该如何解决这个问题?
答案 0 :(得分:2)
我会这样做:
ViewBag.ProjeTipiList = valueSettingsService.GetRuzgarTurbunProjeTipiList().OrderBy(t => t.ProjeTipKod).ToList();
@Html.DropDownListFor(model => model.ProjeTipId, new SelectList(ViewBag.ProjeTipiList,"ProjeTipKod","Id", "...Seçiniz...") ,"Select One")
答案 1 :(得分:1)
因为DropDownList不接受字符串列表,所以它接受IEnumerable<SelectListItem>.
@Html.DropDownListFor(model => model.ProjeTipId, ((List<string>)ViewBag.ProjeTipiList).Select(m => new SelectListItem { Text = m, Value = m }))