コード例 #1
0
        public ActionResult DeleteConfirmed(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var context = new TelesalesScheduleDbContext())
            {
                // Get user from database
                var user = context.Users
                           .Where(u => u.Id.Equals(id))
                           .First();

                // when deleting user to change IsDeleted = true in Employee
                var employee = context.Employees.SingleOrDefault(e => e.UserName == user.UserName);
                employee.IsDeleted            = true;
                context.Entry(employee).State = EntityState.Modified;

                // Delete user and save changes
                context.Users.Remove(user);
                context.SaveChanges();

                this.AddNotification("User deleted.", NotificationType.WARNING);

                return(RedirectToAction("List"));
            }
        }
コード例 #2
0
        public ActionResult Edit(EditEmployeeViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                using (var context = new TelesalesScheduleDbContext())
                {
                    var employee = context.Employees
                                   .FirstOrDefault(e => e.Id == viewModel.Id);

                    if (employee == null)
                    {
                        return(HttpNotFound());
                    }

                    employee.FullName         = viewModel.FullName;
                    employee.UserName         = viewModel.UserName;
                    employee.Manager          = context.Employees.FirstOrDefault(m => m.FullName == viewModel.ManagerFullName);
                    employee.BirthDay         = viewModel.BirthDay;
                    employee.FullTimeAgent    = viewModel.FullTimeAgent;
                    employee.SaveDeskAgent    = viewModel.SaveDeskAgent;
                    employee.SeniorSpecialist = viewModel.SeniorSpecialist;
                    employee.IsDeleted        = viewModel.IsDeleted;

                    context.Entry(employee).State = EntityState.Modified;
                    context.SaveChanges();

                    this.AddNotification("Employee edited.", NotificationType.INFO);

                    return(RedirectToAction("List"));
                }
            }

            return(View(viewModel));
        }
コード例 #3
0
        public ActionResult Edit(Computer computer)
        {
            if (ModelState.IsValid)
            {
                using (var context = new TelesalesScheduleDbContext())
                {
                    context.Entry(computer).State = EntityState.Modified;
                    context.SaveChanges();

                    this.AddNotification("Computer edited.", NotificationType.INFO);

                    return(RedirectToAction("List"));
                }
            }

            return(View(computer));
        }
コード例 #4
0
        public ActionResult Edit(string id, EditUserViewModel viewModel)
        {
            // Check if model is valid
            if (ModelState.IsValid)
            {
                using (var context = new TelesalesScheduleDbContext())
                {
                    // Get user from database
                    var user = context.Users.FirstOrDefault(u => u.Id == id);

                    // Check if user exists
                    if (user == null)
                    {
                        return(HttpNotFound());
                    }

                    // If password field is not empty, change password
                    if (!string.IsNullOrEmpty(viewModel.Password))
                    {
                        var hasher       = new PasswordHasher();
                        var passwordHash = hasher.HashPassword(viewModel.Password);
                        user.PasswordHash = passwordHash;
                    }

                    // Set user properties
                    user.Email    = viewModel.User.Email;
                    user.UserName = viewModel.User.Email;
                    this.SetUserRoles(viewModel, user, context);

                    // Save changes
                    context.Entry(user).State = EntityState.Modified;
                    context.SaveChanges();

                    this.AddNotification("User edited", NotificationType.INFO);

                    return(RedirectToAction("List"));
                }
            }

            return(View(viewModel));
        }