How to CRUD Application Users in ASP.NET MVC 5 wit

2019-05-30 14:00发布

From my experience, I can generate Controllers and Views for CRUD using EntityFrameWork(MVC 5 Controllers with Views, using EntityFramework) for Job and any other classes that are NOT derived from IdentityUser. I want to have a feature in my application where User of Role admin will be able to delete other users in the same User Table. How can I generate Controllers and Views for that using EF ? Any help would be 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; }
}

}

1条回答
你好瞎i
2楼-- · 2019-05-30 14:18

There are pretty good blog posts around, talking about this very specific topic. I'll give you a quick headsup on how and where to start.

I am taking John Attens example postes here as reference.

Since you already have a User class, you can go right into your AccountController and implement an index method. You'd want to display all users first, so you can choose which one you want to delete.

[Authorize(Roles = "Admin")]
public ActionResult Index()
{
    var Db = new ApplicationDbContext();
    var users = Db.Users;
    //ViewModel will be posted at the end of the answer
    var model = new List<EditUserViewModel>();
    foreach(var user in users)
    {
        var u = new EditUserViewModel(user);
        model.Add(u);
    }
    return View(model);
}

From there you can implement the delete methods (GET and 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 with an 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 to implement jobs too, if you want to display them in your index view
}

*AGAIN: This is not my own code. It is an example written by John Atten at http://typecastexception.com. *

查看更多
登录 后发表回答