コード例 #1
0
        //Get
        public ActionResult EditRoles(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
                return new HttpStatusCodeResult(HttpStatusCode.OK);

            var user = db.Users.Find(id);
            AdminUserViewModel AdminModel = new AdminUserViewModel();
            var selected = id.ListUserRoles();
            AdminModel.Roles = new MultiSelectList(db.Roles, "Name", "Name", selected);
            AdminModel.User = user;

            return PartialView(AdminModel);
        }
コード例 #2
0
        // GET: UserRoles
        public ActionResult EditUser(string id)
        {
            var user = db.Users.Find(id);
            var selected = user.Roles.Select(r => r.RoleId);

            AdminUserViewModel adminModel = new AdminUserViewModel() {
                Roles = new MultiSelectList(db.Roles, "Id", "Name", selected),
                UserList = new SelectList(db.Users, "Id", "Name", selected),
                User = user
            };

            return View(adminModel);
        }
コード例 #3
0
 public ActionResult EditUser(string[] selectedRoles, AdminUserViewModel model)
 {
     var rolesBeforeAdd = model.User.Id.ListUserRoles();
     foreach(string role in rolesBeforeAdd)
     {
         if(!selectedRoles.Contains(role))
         {
             model.User.Id.RemoveUserFromRole(role);
         }
     }
     foreach(string role in selectedRoles)
     {
         model.User.Id.AddUserToRole(role);
     }
     return RedirectToAction("Index");
 }
コード例 #4
0
        public ActionResult EditUser(string id, AdminUserViewModel model)
        {
            if (id == null) {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var user = db.Users.Find(id);
            UserRolesHelper helper = new UserRolesHelper();

            foreach (var role in db.Roles.ToList()) {
                //If selected but user doesn't have, add
                if (model.SelectedRoles.Contains(role.Id)) {
                    helper.AddUserToRole(id, role.Name);
                }
                //If not selected but user *does* have, remove
                else if (!model.SelectedRoles.Contains(role.Id)) {
                    helper.RemoveUserFromRole(id, role.Name);
                }
            }

            return RedirectToAction("Index", new { id });
        }