【问题标题】:Adding a controller for ApplicationUser.cs? MVC Core为 ApplicationUser.cs 添加控制器? MVC 核心
【发布时间】:2017-04-23 10:24:23
【问题描述】:

我正在尝试为 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 模型类添加控制器时,控制器被创建(包括视图文件)但无法启动。控制器是新建控制器后的默认代码。

【问题讨论】:

  • “启动失败”是什么意思?您可以添加您的控制器代码(相关位)和异常消息吗?
  • 为什么不直接生成一个新的默认模板项目并从中复制 AccountController.cs?

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


【解决方案1】:

你完全走上了正轨,Dean。 但是,我将向您介绍如何手动创建控制器,以及如何创建编辑方法。随意检查您生成的控制器是否有任何不匹配,或者更好的是,完全按照这些步骤操作。

首先:我建议您创建一个仅包含您打算向管理员公开的属性的 Viewmodel(在一个新文件中)。此视图模型可能如下所示。

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

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

下一步:您应该创建一个控制器,例如 AdminControllerlike so。

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

【讨论】:

  • 感谢 Temi 提供详细的演练。
  • 在字段元素中输入所有数据并选择保存按钮后,我收到以下错误消息:处理请求时发生未处理的异常。 ArgumentNullException:值不能为空。参数名称:keyValues Microsoft.EntityFrameworkCore.Internal.EntityFinder.FindTracked(Object[] keyValues, out IReadOnlyList keyProperties) ProjectTest1.Controllers.UserViewModelsController+d__7.MoveNext() in UserViewModelsController.cs + var user = await _userManager。 FindByIdAsync(Id);
  • 我明白了。这可能是因为 id 没有作为参数传递给 EditUser 控制器操作。由于很多原因,我通常不会将用户 ID 暴露给视图;因此,我在没有 ID 的情况下传入 Userviewmodel 的原因,然后将 Id 作为单独的参数。但是,为了使这个代码示例不那么繁琐,我对代码示例进行了编辑,改为通过电子邮件获取用户。
  • 谢谢特米。我终于弄清楚为什么没有传递用户 ID,那是因为我没有向 HTTPGet Edit 添加方法
  • 很棒的院长。很高兴你做到了??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-14
  • 1970-01-01
  • 2020-11-02
  • 2019-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多