c# - Fetching data from database and populating a dropdownlist from a List<object> in MVC -


i have model:

public partial class userrole {     public int id { get; set; }     public int userid { get; set; }     public int roleid { get; set; }     public int status { get; set; }      public virtual user users { get; set; }     public virtual role roles { get; set; } }  public partial class user {     public user()     {         roles = new list<selectlistitem>();     }      public long id { get; set; }     public string email { get; set; }     public string password { get; set; }     public system.datetime reg_date { get; set; }     public byte validated { get; set; }     public virtual icollection<userrole> userroles { get; set; }     public int roleid { get; set; }     public string rolename { get; set; }      public ienumerable<selectlistitem> roles { get; set; }      //public ienumerable<role> roles { get; set; } }  public partial class role {     public int id { get; set; }     public string rolename { get; set; }     public string desc { get; set; }     public int status { get; set; }     public virtual icollection<userrole> userroles { get; set; } } 

controller load view:

public actionresult assignrole(long id = 0)     {         user usermodel = new user();         int userid = convert.toint32(id);         ilist<user> users = new list<user>();         var query = role in db.roles                     join userrole in db.userroles on role.id equals userrole.roleid                     join user in db.users on userrole.userid equals user.id                     userrole.userid == userid                     select new { rolename  = role.rolename, email = user.email, userid = user.id };            var userss = query.tolist();         foreach(var userlist in userss)         {             users.add(new user()                 {                     id = userlist.userid,                     email = userlist.email,                     rolename = userlist.rolename                 });         }          loadroles(users);         return view(users);     }  public void loadroles(ilist<user> model)     {          var roles = db.roles.asqueryable<role>().select(x => new selectlistitem()         {              text = x.rolename,             value = sqlfunctions.stringconvert((double) x.id)         }).tolist();          var usermodel = new user()         {             roles = roles.tolist()         };      } 

i able other values linq in first part of view unable populate dropdownlist.

<fieldset> <div>     <table>         <tr>             <th>                 @html.displaynamefor(model => model.email)             </th>             <th>                 @html.displaynamefor(model => model.rolename)             </th>             <th></th>         </tr>         @foreach (var item in model)         {             <tr>                 <td>                     @html.displayfor(modelitem => item.email)                 </td>                 <td>                     @html.displayfor(modelitem => item.rolename)                 </td>                 <td>                     @html.actionlink("delete", "delete", new { id = item.id })                 </td>             </tr>         }     </table> </div>  <div class="display-label">     @html.displayname("add role") </div>  @foreach (var item in model) {     <div class="display-field">         @html.dropdownlistfor(modeiteml => item.roleid, item.roles)     </div> }  <p>     <input type="submit" value="assign" /> </p> 

how can populate dropdownlist? have tried still no luck.

you'll find clearer if write this:

public actionresult assignrole(int id = 0) {     var user = (from u in db.users                join r in db.userroles on r.userid equals u.id g                u.id == id                select new user                {                    id = u.id,                    email = u.email,  // assign properties normal select.                    roles = g.select(x => new role { rolename = x.rolename ... })                })                .firstordefault();      return view(user); // update view expect single user } 

this way you've gotten need in 1 quick trip database, , model represents have (a single user multiple roles).

if want separate list of roles can assign, recommend putting in separate view model scenario. i.e.

public class assignrolesmodel  {     public user user { get; set; }     public ienumerable<role> availableroles { get; set; } } 

then change return view(user) to:

var viewmodel = new assignrolesmodel                {                    user = user,                    roles = db.roles                              .select(x =>                                  new selectlistitem()                                 {                                     text = x.rolename,                                     value = sqlfunctions.stringconvert((double) x.id)                                 })                              .tolist()                };  return view(viewmodel); 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -