c# - How to CRUD Application Users in ASP.NET MVC 5 with Scaffolding? -


from experience, can generate controllers , views crud using entityframework(mvc 5 controllers views, using entityframework) job , other classes not derived identityuser. want have feature in application user of role admin able delete other users in same user table. how can generate controllers , views using ef ? appreciated. thank you!

public class user : identityuser {     public string firstname { get; set; }     public string lastname { get; set; }     public icollection<job> jobs { get; set; } }  public class job {     public int id jobid {get; set;}     public string jobdetails {get; set;}     public bool isdone {get; set;}     public virtual user user {get; set;} }  public class applicationdbcontext : identitydbcontext<user> {     public applicationdbcontext()         : base("defaultconnection")     {     }     public dbset<job> jobs { get; set; } } 

}

there pretty blog posts around, talking specific topic. i'll give quick headsup on how , start.

i taking john attens example postes here reference.

since have user class, can go right accountcontroller , implement index method. you'd want display users first, can choose 1 want delete.

[authorize(roles = "admin")] public actionresult index() {     var db = new applicationdbcontext();     var users = db.users;     //viewmodel posted @ end of answer     var model = new list<edituserviewmodel>();     foreach(var user in users)     {         var u = new edituserviewmodel(user);         model.add(u);     }     return view(model); } 

from there can implement delete methods (get , post):

[authorize(roles = "admin")] public actionresult delete(string id = null) {     var db = new applicationdbcontext();     var user = db.users.first(u => u.username == id);     var model = new edituserviewmodel(user);     if (user == null)     {         return httpnotfound();     }     return view(model); }   [httppost, actionname("delete")] [validateantiforgerytoken] [authorize(roles = "admin")] public actionresult deleteconfirmed(string id) {     var db = new applicationdbcontext();     var user = db.users.first(u => u.username == id);     db.users.remove(user);     db.savechanges();     return redirecttoaction("index"); } 

edituserviewmodel

public class edituserviewmodel {     public edituserviewmodel() { }      // allow initialization instance of applicationuser:     public edituserviewmodel(applicationuser user)     {         this.username = user.username;         this.firstname = user.firstname;         this.lastname = user.lastname;         this.email = user.email;     }      [required]     [display(name = "user name")]     public string username { get; set; }      [required]     [display(name = "first name")]     public string firstname { get; set; }      [required]     [display(name = "last name")]     public string lastname { get; set; }      [required]     public string email { get; set; }      //you might want implement jobs too, if want display them in index view } 

*again: not own code. example written john atten @ http://typecastexception.com. *


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

angularjs - ng-repeat duplicating items after page reload -