Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否为ApplicationUser.cs添加控制器?MVC核心_C#_Asp.net Mvc_Asp.net Core_Visual Studio 2017 - Fatal编程技术网

C# 是否为ApplicationUser.cs添加控制器?MVC核心

C# 是否为ApplicationUser.cs添加控制器?MVC核心,c#,asp.net-mvc,asp.net-core,visual-studio-2017,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

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

您完全正常了。 但是,我将向您介绍如何手动创建控制器,以及如何创建编辑方法。请随意检查生成的控制器是否存在任何不匹配,或者更好的是,完全按照以下步骤操作

首先:我建议您创建一个Viewmodel(在一个新文件中),该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; }
}
请注意,使用视图模型是一种更标准化的方法,因此可以跳过此步骤。

下一步:您应该像这样创建一个控制器,比如
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");
    }
}
[Authorize(Roles=“Admin”)]
公共类AdminController:Controller
{
私有只读应用程序DBContext\u dataContext;
私有只读用户管理器_UserManager;
公共AdminController(ApplicationDbContext数据上下文、UserManager用户管理器、IHostingEnvironment环境)
{
//注入datacontext和userManager依赖项
_dataContext=dataContext;
_userManager=userManager;
}
//用于编辑用户的HTTPGET控制器操作
[HttpGet]
公共IActionResult EditUser()
{
返回视图();
}
//用于编辑用户的HTTPPOST控制器操作
[HttpPost]
公共异步任务EditUser(UserViewModel模型)
{
//通过传入的电子邮件获取用户。
/*更好的做法是通过Id查找用户(不向视图公开Id)。
然而,为了简化这个示例,我们可以通过电子邮件找到用户*/
var user=await\u userManager.findbyemailsync(model.Email);
//编辑用户:替换UserViewModel属性的值
user.Summary=model.Summary;
user.FirstName=model.FirstName;
user.LastName=model.LastName;
user.RoleType=model.RoleType;
user.Address=model.Address;
user.City=model.City;
//将用户添加到datacontext(数据库)并保存更改
_dataContext.Update(用户);
wait_dataContext.saveChangesSync();
返回重定向到操作(“操作名称”、“控制器名称”);
//这可能是
//返回重定向到操作(“索引”、“主页”);
}
}

感谢Temi的详细演练。在字段元素中输入所有数据并选择“保存”按钮后,我收到以下错误消息:处理请求时发生未处理的异常。ArgumentNullException:值不能为null。参数名称:keyValues Microsoft.EntityFrameworkCore.Internal.EntityFinder.FindTracked(对象[]keyValues,out IReadOnlyList keyProperties)ProjectTest1.Controller.UserViewModelsController中的UserViewModelsController+d_u7.MoveNext(),UserViewModelsController.cs+var user=Wait\u userManager.FindByIdAsync(Id);我懂了。这可能是因为id没有作为参数传递到EditUser控制器操作中。由于很多原因,我通常不向视图公开用户ID;因此,我在没有ID的情况下传入Userviewmodel,然后将ID作为单独的参数传入。然而,为了使这个代码示例稍微不那么麻烦,我对代码示例进行了编辑,改为通过电子邮件获得用户。谢谢Temi。我终于明白了为什么没有传递userID,这是因为我没有向HTTPGet编辑器添加方法。很高兴你做到了你说的“发射失败”是什么意思?您可以添加控制器代码(相关位)和异常消息吗?为什么不生成一个新的默认模板项目并从中复制AccountController.cs?