1 回答

TA貢獻1712條經驗 獲得超3個贊
這就是我目前想出的。它尚未 100% 完成,但我可以獲得一個用戶并將該用戶分配給管理員,它肯定可以與 1 個用戶一起使用,我必須測試多個用戶。我會對我正在做的事情的任何輸入或評論感興趣。謝謝
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AssignAdmin(AssignRolesModel model)
{
try
{
if(model.lstUsers == null)
{
TempData["Error"] = "There are no Users to Assign Roles";
model.lstUsers = UserList();
model.lstAdmins = AdminList();
return View(model);
}
var selectedUsersCount = (from user in model.lstUsers
where user.SelectedUsers == true
select user).Count();
if(selectedUsersCount == 0)
{
TempData["Error"] = "You have not Selected any User to Assign Roles";
model.lstAdmins = AdminList();
model.lstUsers = UserList();
return View(model);
}
if (ModelState.IsValid)
{
List<UserModel> users = new List<UserModel>();
ApplicationUser au;
var store = new UserStore<ApplicationUser>(context);
var manager = new UserManager<ApplicationUser>(store);
model.CreatedBy = 1;
foreach(var u in model.lstUsers)
{
if(u.SelectedUsers == true)
{
users.Add(u);
}
}
foreach(var u in users)
{
au = context.Users.Where(x => x.Id.Equals(u.UserId,
StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
manager.AddToRole(au.Id, "Admin");
}
TempData["Success"] = "Roles Assigned Successfully";
return RedirectToAction("AssignAdmin");
}
}
catch (Exception)
{
throw;
}
return View();
}
- 1 回答
- 0 關注
- 130 瀏覽
添加回答
舉報