public ActionResult EditAnExistingUser(string id) { EditUser UserToBeEdited = new EditUser(); try { if (id != null) { var presentUserToBeEdited = from usertable in database.DX_USER where usertable.userid == id select usertable; if (presentUserToBeEdited != null && presentUserToBeEdited.ToList().Count() == 1) { DX_USER user = (DX_USER)presentUserToBeEdited.ToList().First(); UserToBeEdited.FirstName = user.fname; UserToBeEdited.LastName = user.lname; UserToBeEdited.Email = user.userid; UserToBeEdited.Position = user.role; List<int> depts = DbCommonQueries.getDepartmentIds(user.userid, database); UserToBeEdited.Department = depts; UserToBeEdited.AccessLevel = user.accesslevel; } } populateDepartmenetsList(); } catch { ModelState.AddModelError("", "Error occured while editing existing user"); } return View(UserToBeEdited); }
public ActionResult EditAnExistingUser(EditUser model) { try { var editedUser = from usertable in database.DX_USER where usertable.userid == model.Email select usertable; if (editedUser != null && editedUser.ToList().Count() == 1) { //var DepartmentRecord = alldepartments.First(); DX_USER user = (DX_USER)editedUser.ToList().First(); user.accesslevel = model.AccessLevel; user.role = model.Position; database.ObjectStateManager.ChangeObjectState(user, EntityState.Modified); } var userCurrentdepartments = from usertable in database.DX_USERDEPT where usertable.userid == model.Email select usertable; if (userCurrentdepartments != null && userCurrentdepartments.ToList().Count >= 1) { foreach (DX_USERDEPT userdepartment in userCurrentdepartments.ToList()) { // DX_USERDEPT userDept = new DX_USERDEPT(); database.DX_USERDEPT.DeleteObject(userCurrentdepartments.ToList().First()); int success = database.SaveChanges(); } var alldepartment = from usertable in database.DX_DEPARTMENT where model.Department.Contains(usertable.deptid) select usertable; if (alldepartment != null && alldepartment.ToList().Count() >= 1) { foreach (DX_DEPARTMENT dept in alldepartment.ToList()) { DX_USERDEPT userDept = new DX_USERDEPT(); userDept.deptid = dept.deptid; userDept.userid = model.Email; database.DX_USERDEPT.AddObject(userDept); int success = database.SaveChanges(); } } } } catch { ModelState.AddModelError("", "Error while updating user details"); } return RedirectToAction("AllExistingUsers"); }