asp.net mvc - Issues converting types to new selectitemlist -


vs'12 asp.net c# mvc4 ef code first

in controller

var usernames = roles.getusersinrole("admin"); var adminusers = db.userprofiles                  .where(x => !usernames.contains(x.username)).tolist();  list<userprofiles> mylist = adminusers.tolist(); ienumerable<userprofiles> myenumerable = mylist;  viewbag.ddlroles = myenumerable; 

the error coming view states

the viewdata item has key 'ddlroles' of type 'system.collections.generic.list'1[[og.models.userprofiles, og, version=1.0.0.0, culture=neutral, publickeytoken=null]]' must of type 'ienumerable<selectlistitem>'.

and have been trying convert query ienumerable<selectlistitem> crazy, if can me convert right way i'd appreciate alot.

also if did not make clear, i'm doing taking list of users linq query not of "admin" roles , trying display them inside of dropdrownlist

edit: the view

@model og.models.userprofiles

then later

@html.dropdownlist("ddlroles", (selectlist)viewdata["selectedvalue"])

your view should this:

@model og.models.userprofiles  @html.dropdownlistfor(model => model.userid, new selectlist(viewbag.ddlroles, "userid", "username", model.userid)) 

and, in controller, you'll have:

var usernames = roles.getusersinrole("admin"); var adminusers = db.userprofiles                  .where(x => !usernames.contains(x.username)).tolist();  viewbag.ddlroles = adminusers; 

this submit userid of selected user dropdownlist action. way, please choose more meaningful names models. "userprofiles" model should called "userprofile" or "user", because represents 1 user only, not collection of users.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -