コード例 #1
0
        public ActionResult Edit(int? id)
        {
            UsersService usersService = new UsersService();

            UsersEditVM model = new UsersEditVM();
            User user;

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

            model.ID = user.ID;
            model.Username = user.Username;
            model.FirstName = user.FirstName;
            model.LastName = user.LastName;
            model.Email = user.Email;
            model.UserRole = user.UserRole;
            model.CityID = user.CityID;

            //model.Countries = new CountriesRepository().GetAll(); 1
            //model.Countries = new SelectList(new CountriesRepository().GetAll(), "ID", "Name"); //2
            model.Cities = usersService.GetSelectedCities(); // 3
            model.Teams = usersService.GetSelectedTeams(user.Teams);

            return View(model);
        }
コード例 #2
0
        public ActionResult Edit()
        {
            UnitOfWork unitOfWork = new UnitOfWork();
            UsersService usersService = new UsersService(unitOfWork);

            UsersEditVM model = new UsersEditVM();
            TryUpdateModel(model);

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

            if (!ModelState.IsValid)
            {
                model.Cities = usersService.GetSelectedCities();
                model.Teams = usersService.GetSelectedTeams(user.Teams, model.SelectedTeams);

                return View(model);
            }

            user.ID = model.ID;
            user.Username = model.Username;
            user.FirstName = model.FirstName;
            user.LastName = model.LastName;
            user.Email = model.Email;
            user.UserRole = model.UserRole;
            user.CityID = model.CityID;

            usersService.UpdateUserTeams(user, model.SelectedTeams);

            usersService.Save(user);

            return RedirectToAction("List");
        }