Example #1
0
 public ActionResult EditUserRole(string userId, AdminViewModel adminModel)
 {
     UserRoleHelpers helper = new UserRoleHelpers();
     if(ModelState.IsValid)
     {
         string[] empt = { };
         adminModel.SelectedRoles = adminModel.SelectedRoles ?? empt;
         foreach (var role in db.Roles.ToList())
         {
             //If selected but user doesn't have, add
             if (adminModel.SelectedRoles.Contains(role.Name))
             {
                 helper.AddUserToRole(userId, role.Name);
             }
             //If not selected remove
             else
             {
                 helper.RemoveuserFromRole(userId, role.Name);
             }
         }
     }
     return RedirectToAction("AdminIndex");
 }
Example #2
0
        public ActionResult EditUserRole(string userId)
        {
            var user = db.Users.Find(userId);
            AdminViewModel adminModel = new AdminViewModel();
            // Creating an object of helper class
            UserRoleHelpers helper = new UserRoleHelpers();
            // Calling Helper Method
            var selected = helper.ListUserRoles(userId);
            adminModel.Roles = new MultiSelectList(db.Roles, "Name", "Name", selected);
            adminModel.User = user;

            return View(adminModel);
        }