为ApplicationUser.cs添加控制器? MVC核心

时间:2017-04-23 10:24:24

标签: c# asp.net-mvc asp.net-core visual-studio-2017

我正在尝试为ApplicationUser.cs添加一个控制器,以便在管理员登录时,他们能够查看,编辑,删除dbo.AspNetUsers表中的任何记录,但我认为我在做错了。有什么想法吗?

这是ApplicationUser.cs的代码:

namespace ProjectTest.Models
{
    // Add profile data for application users by adding properties to the ApplicationUser class
    public class ApplicationUser : IdentityUser
    {
        public string Summary { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        override public string UserName { get; set; }
        public string RoleType { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        override public string PhoneNumber { get; set; }
        override public string Email { get; set; }
    }
}

当我尝试为ApplicationUser模型类添加控制器时,控制器被创建(包括视图文件)但无法启动。创建新控制器后,控制器是默认代码。

1 个答案:

答案 0 :(得分:1)

你完全走上了正轨Dean。 但是,我将指导您如何手动创建控制器,并创建编辑方法。请随意检查生成的控制器是否存在任何不匹配,或者更好地完全按照这些步骤进行检查。

首先关闭:我建议您创建一个Viewmodel(在新文件中),该Viewmodel只包含您要向管理员公开的属性。此View模型可能如下所示。

public class UserViewModel
{    
    public string Summary { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string RoleType { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    override public string PhoneNumber { get; set; }
}

请注意,使用视图模型是一种更标准化的方法,因此请跳过此步骤。

下一步:你应该创建一个像AdminController这样的控制器。

[Authorize(Roles = "Admin")]
public class AdminController : Controller
{
     private readonly ApplicationDbContext _dataContext;
     private readonly UserManager<ApplicationUser> _userManager;

    public AdminController(ApplicationDbContext dataContext, UserManager<ApplicationUser> userManager, IHostingEnvironment environment)
    {
        // Inject the datacontext and userManager Dependencies
        _dataContext = dataContext;
        _userManager = userManager;
    }

    // HTTPGET Controller action to edit user
    [HttpGet]
    public IActionResult EditUser()
    {
        return View();
    }

     // HTTPPOST Controller action to edit user
    [HttpPost]
    public async Task<IActionResult> EditUser(UserViewModel model)
    {
        //Get User by the Email passed in.
        /*It's better practice to find user by the Id, (without exposing the id to the view).
        However, to keep this example simple, we can find the user by email instead*/
        var user = await _userManager.FindByEmailAsync(model.Email);

        //edit user: replace values of UserViewModel properties 
        user.Summary = model.Summary;
        user.FirstName = model.FirstName;
        user.LastName = model.LastName;
        user.RoleType = model.RoleType;
        user.Address = model.Address;
        user.City = model.City;

         //add user to the datacontext (database) and save changes
        _dataContext.Update(user);
        await _dataContext.SaveChangesAsync();

        return RedirectToAction("ACTION_NAME", "CONTROLLER_NAME");
        //this could be
        //return RedirectToAction("Index", "Home");
    }
}