Пример #1
0
        public ActionResult Edit(int? id)
        {
            User user;
            UserRepository userRep = new UserRepository();

            if (!id.HasValue)
            {
                user = new User();
            }
            else
            {
                user = userRep.GetByID(id.Value);
                if (user == null)
                {
                    return RedirectToAction("List");
                }
            }

            UsersEditVM model = new UsersEditVM();
            model.ID = user.ID;
            model.Username = user.Username;
            model.FirstName = user.FirstName;
            model.LastName = user.LastName;
            model.Email = user.Email;
            model.Groups = PopulateAssignedGroups(user);

            return View(model);
        }
Пример #2
0
        public ActionResult Edit()
        {
            UsersEditVM model = new UsersEditVM();
            TryUpdateModel(model);

            UserRepository userRep = new UserRepository(unitOfWork);

            string selectedGroups = Request.Form["assignedGroups"];
            string[] assignedGroups;
            if (selectedGroups == null)
            {
                assignedGroups = new string[0];
            }
            else
            {
                assignedGroups = selectedGroups.Split(',');
            }

            User user;
            if (model.ID == 0)
            {
                user = new User();
            }
            else
            {
                user = userRep.GetByID(model.ID);
                if (user == null)
                {
                    return RedirectToAction("List");
                }
            }

            if (!ModelState.IsValid)
            {
                model.Groups = PopulateAssignedGroups(user);

                return View(model);
            }

            user.ID = model.ID;
            user.Username = model.Username;
            user.Password = model.Password;
            user.FirstName = model.FirstName;
            user.LastName = model.LastName;
            user.Email = model.Email;
            UpdateUserGroups(user, assignedGroups);

            userRep.Save(user);
            unitOfWork.Commit();

            return RedirectToAction("List");
        }