MVC保存修改角色的更改

时间:2017-01-13 10:02:04

标签: c# model-view-controller roles

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

以下是用于编辑保存更改的[HTTPPPOST]代码

  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

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

这就是这样,但我未能保存这个

IMAGES SAMPLE EDIT ROLES

我打算让这个用户成为管理员,但我失败了,如何解决这个问题?非常感谢你

0 个答案:

没有答案