C# MVC保存修改角色的更改

C# MVC保存修改角色的更改,c#,model-view-controller,roles,C#,Model View Controller,Roles,我将保存所选用户管理员对修改角色所做的更改 下面是编辑保存更改的[HttpPost]代码 public ActionResult Edit(UserViewModel model,ApplicationUser x, Dictionary<string, IdentityRole> roles) { var tes = string.Join(",", x.Roles.Select(y => roles[y.RoleId].Name));

我将保存所选用户管理员对修改角色所做的更改

下面是编辑保存更改的[HttpPost]代码

  public ActionResult Edit(UserViewModel model,ApplicationUser x, Dictionary<string, IdentityRole> roles)
    {
        var tes = string.Join(",", x.Roles.Select(y => roles[y.RoleId].Name));
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser {
                UserName = model.Username,
                Email = model.Username,
            };
            //var result = await UserManager.CreateAsync(user, model.Password);
            //if (result.Succeeded)
            //{
            //    return RedirectToAction("Index");
            //}
            //AddErrors(result);

            //model.UserId = x.Id;
            //model.Username = x.Email;
            //model.Roles = string.Join(",",x.Roles.Select(y=> roles[y.RoleId].Name));
            _context.Entry(model).State = EntityState.Modified;
            _context.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(model);
    }
公共操作结果编辑(UserViewModel模型、ApplicationUser x、字典角色) { var tes=string.Join(“,”,x.Roles.Select(y=>Roles[y.RoleId].Name)); if(ModelState.IsValid) { var user=新应用程序用户{ UserName=model.UserName, Email=model.Username, }; //var result=await UserManager.CreateAsync(用户、模型、密码); //if(result.successed) //{ //返回操作(“索引”); //} //加法器(结果); //model.UserId=x.Id; //model.Username=x.Email; //model.Roles=string.Join(“,”,x.Roles.Select(y=>Roles[y.RoleId].Name)); _context.Entry(model.State=EntityState.Modified; _SaveChanges(); 返回操作(“索引”); } 返回视图(模型); } 这就是我的USERVIEWMODEL

 private UserViewModel ConvertToUserViewModel(ApplicationUser x, Dictionary<string, IdentityRole> roles)
    {
        return new UserViewModel()
        {
            UserId = x.Id,
            Username = x.Email,
            Roles = string.Join(",", x.Roles.Select(y => roles[y.RoleId].Name))
        };
    }
private UserViewModel ConvertToUserViewModel(应用程序用户x,字典角色)
{
返回新的UserViewModel()
{
UserId=x.Id,
用户名=x.电子邮件,
Roles=string.Join(“,”,x.Roles.Select(y=>Roles[y.RoleId].Name))
};
}
这将是什么样子,但我没能挽救这一个

我要让这个用户作为管理员,但我失败了,如何解决这个问题?多谢各位