本文介绍了如何CRUD应用程序用户在ASP.NET MVC 5脚手架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的经验,我就可以使用的EntityFramework(MVC 5控制器与意见,使用的EntityFramework)的工作和不是从IdentityUser派生的任何其他类的CRUD控制器和视图。我想在我的应用程序一个功能,管理员角色的用户将能够在同一个用户表中删除其他用户。我怎么能生成控制器和视图对于使用EF?任何帮助将是AP preciated。谢谢!

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; }
}

}

推荐答案

有pretty好的博客文章周围,谈论这个非常特殊的话题。我给你如何和从哪里开始快速headsup。

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.

在这里,我以约翰Attens例如postes为参考。

I am taking John Attens example postes here as reference.

既然你已经有一个用户类,你可以去到您的权利的AccountController和实施指数法。你要首先显示所有用户,所以你可以选择你想删除哪一个。

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);
}

从那里,你可以实现删除方法(GET和POST):

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:这不是我自己的code。正是在 http://typecastexception.com 通过约翰安泰信写一个例子。 *

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

这篇关于如何CRUD应用程序用户在ASP.NET MVC 5脚手架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:55