Wednesday, 20 September 2017

Cannot convert type 'System.Collections.Generic.List' to 'System.Web.Mvc.SelectList'

Because DropDownList does not accept a list of strings. It accepts IEnumerable<SelectListItem>. It's your responsibility to convert your list of strings into that. This is easy enough though:
domains.Select(m => new SelectListItem { Text = m, Value = m })
Then, you can feed that to DropDownList:
@Html.DropDownList("domains", ((List<string>)ViewBag.domains).Select(m => new SelectListItem { Text = m, Value = m }))

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Blog Archive