public ActionResult Edit(long id) { ActivityRepository repo = new ActivityRepository(); var activity = repo.GetByID(id); ActivityEditVM model = new ActivityEditVM() { ID = activity.ReportID, ActivityID = activity.ID, Description = activity.Description, TargetDate = activity.TargetDate }; return(View(model)); }
public ActionResult ActivityEdit(int professorId, int activityId) { Activity activity = professorServices.GetActivity(professorId, activityId); List <ActivityType> allActivityTypes = activityServices.GetAllActivityTypes().ToList(); List <int> selectedStudents = activity.StudentsLink.Select(studentActivity => studentActivity.Student.Id).ToList(); List <int> otherStudents = studentServices.GetAllStudents().Select(student => student.Id).ToList(); otherStudents.RemoveAll(student => selectedStudents.Contains(student)); ActivityEditVM activityEdit = new ActivityEditVM { Id = activity.Id, Name = activity.Name, Description = activity.Description, ActivityTypeId = activity.ActivityType.Id, OccurenceDates = activity.OccurenceDates.Select(occurenceDate => occurenceDate.OccurenceDate).ToList(), ActivityTypes = allActivityTypes, SelectedStudentsId = selectedStudents, OtherStudentsId = otherStudents }; return(View(activityEdit)); }
public ActionResult ActivityEdit(int professorId, ActivityEditVM activityModel) { List <ActivityType> allActivityTypes = activityServices.GetAllActivityTypes().ToList(); Activity activity = activityServices.GetActivity(activityModel.Id); Activity editedActivity = new Activity { Id = activityModel.Id, Name = activityModel.Name, Description = activityModel.Description, ActivityType = allActivityTypes.Find(actType => actType.Id == activityModel.ActivityTypeId), OccurenceDates = activity.OccurenceDates }; // good job if (activityModel.OccurenceDates != null) { List <DateTime> newActivityDates = null; newActivityDates = activityModel.OccurenceDates.ToList(); List <DateTime> oldDates = activity.OccurenceDates.Select(occurence => occurence.OccurenceDate).ToList(); newActivityDates.RemoveAll(date => oldDates.Contains(date)); foreach (var date in newActivityDates) { ActivityOccurence newActivityOccurence = new ActivityOccurence { OccurenceDate = date, Activity = activity }; editedActivity.OccurenceDates.Add(newActivityOccurence); foreach (var link in activity.StudentsLink) { link.Student.ActivityInfos.Add(new ActivityInfo { Student = link.Student.Id, Activity = activity, ActivityId = activity.Id, OccurenceDate = newActivityOccurence }); } } } List <int> newStudents = new List <int>(); List <int> removedStudents = new List <int>(); List <int> oldStudents = activity.StudentsLink.Select(link => link.Student.Id).ToList(); if (activityModel.SelectedStudentsId != null) { newStudents = activityModel.SelectedStudentsId.ToList(); } newStudents.RemoveAll(student => oldStudents.Contains(student)); if (activityModel.SelectedStudentsId != null) { removedStudents = oldStudents.Except(activityModel.SelectedStudentsId).ToList(); } else { removedStudents = oldStudents; } List <StudentActivity> oldStudentActivities = activity.StudentsLink.ToList(); List <Student> removedStudentsEntities = oldStudentActivities.Where(link => removedStudents.Contains(link.Student.Id)) .Select(link => link.Student).ToList(); foreach (var removedStudent in removedStudentsEntities) { List <ActivityInfo> activityInfos = removedStudent.ActivityInfos.ToList(); List <ActivityInfo> removedActivityInfos = activityInfos.Where(act => act.Activity.Id == activity.Id).ToList(); activityInfos.RemoveAll(actInfo => actInfo.Activity.Id == activity.Id); removedStudent.ActivityInfos = activityInfos; foreach (var activityInfoToRemove in removedActivityInfos) { activityServices.RemoveActivityInfo(activityInfoToRemove); } } List <StudentActivity> removedStudentActivity = oldStudentActivities.Where(link => removedStudents.Contains(link.Student.Id)).ToList(); oldStudentActivities.RemoveAll(link => removedStudents.Contains(link.Student.Id)); editedActivity.StudentsLink = oldStudentActivities; foreach (var studentActivityToRemove in removedStudentActivity) { activityServices.RemoveStudentActivity(studentActivityToRemove); } foreach (var newStudent in newStudents) { Student student = studentServices.GetStudent(newStudent); editedActivity.StudentsLink.Add(new StudentActivity { Student = student, Activity = activity }); foreach (var occurence in editedActivity.OccurenceDates) { student.ActivityInfos.Add(new ActivityInfo { Student = student.Id, Activity = activity, ActivityId = activity.Id, OccurenceDate = occurence }); } } professorServices.EditActivity(professorId, editedActivity); return(Redirect($"/Professors/{professorId}/Activities")); }
public ActionResult EditPost(ActivityEditVM model) { var id = model.ID; //Put a breakpoint here to see the bug. return(RedirectToAction("Index", new { id = model.ID })); }