Help with dropdownlist - LINQ

I am trying to create a select list ...that basically just creates a user entry for my user to select options to be inserted into the database.

I have this in my controller...which Is not complete  Here roles is a list of value and label.....What am I missing here..and how would you then use this to display on view....to I need something in my view model as well?

       private IEnumerable<SelectListItem> GetRoles()
        {
            IContactEntityEditService contactEntityEditService = DependencyResolver.Current.GetService<IContactEntityEditService>();
            List<string> roles = contactEntityEditService.GetContactLinkedEntitiesRoles();
           
            roles.Select(x => new SelectListItem
                                {
                                    Value = x.value,
                                    Text = x.label
                                });

            return new SelectList(roles, "Value", "Text");
        }

Open in new window

LVL 11
Robb HillSenior .Net DeveloperAsked:
Who is Participating?
 
Robb HillSenior .Net DeveloperAuthor Commented:
my solution....controller code

  IContactEntityEditService contactEntityEditService = DependencyResolver.Current.GetService<IContactEntityEditService>();
            List<string> roles = contactEntityEditService.GetContactLinkedEntitiesRoles();

            IEnumerable<SelectListItem> selectRoles = roles
                                         .Select(i => new SelectListItem()
                                         {
                                             Text = i.ToString(),
                                             Value = i
                                         });
            ViewBag.DDLRoles = selectRoles;

Open in new window



view:

   @Html.DropDownList("DropDownListID", new SelectList(ViewBag.DDLRoles, "Value", "Text"), "--- Please Choose an Item! ---")

Open in new window

0
 
Dorababu MSenior Software EngineerCommented:
How about this
roles.Select(x => new SelectListItem
{
     Value = x.value,
     Text = x.label
}).ToList();

Open in new window

0
 
Robb HillSenior .Net DeveloperAuthor Commented:
x has no values ...from roles...


 private IEnumerable<SelectListItem> GetRoles()
        {
            IContactEntityEditService contactEntityEditService = DependencyResolver.Current.GetService<IContactEntityEditService>();
            List<string> roles = contactEntityEditService.GetContactLinkedEntitiesRoles();

            roles.Select(x => new SelectListItem
            {
                Value = x.value,
                Text = x.label
            }).ToList();

            return new SelectList(roles, "Value", "Text");
        }

Open in new window

0
 
Robb HillSenior .Net DeveloperAuthor Commented:
my solution works adn shows how to implement with this approach
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.