MVC3 Roles Dropdown box
-
11-06-2021 - |
Question
Im trying add roles to a drop dropdown box but im getting the error thats below. please help.
Model Code
public SelectListItem RolesForUser {get; set;}
Controller Code
var roles = Roles.GetRolesForUser(model.username);
model.RolesForUser=roles.select(m => new SelectListItem()
{Value=m.ToString(),Text=m.ToString()});
Error 21 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Web.Mvc.SelectListItem'. An explicit conversion exists (are you missing a cast?)
Solution
declare RolesForUser as List<SelectListItem> RolesForUser
and change LINQ query as
model.RolesForUser=roles.select(m => new SelectListItem() {Value=m.ToString(),Text=m.ToString()}).ToList();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow