public ActionResult Grade(FormCollection form) { var projectTaskId = 0; int.TryParse(form["projectTaskId"], out projectTaskId); var projectTask = projectTasksRepository.GetProjectTaskById(projectTaskId); // TODO: Check if course exists if (!User.IsInRole("Administrator") && !(coursesRepository.IsTeacherInCourse(projectTask.Project.CourseId, User.Identity.GetUserId(), User.IsInRole("Administrator")) || coursesRepository.IsAssistantTeacherInCourse(projectTask.Project.CourseId, User.Identity.GetUserId(), User.IsInRole("Administrator")))) { throw new HttpException((int)HttpStatusCode.Forbidden, "Forbidden!"); } projectTasksRepository.RemoveAllGradesForProjectTask(projectTask); foreach (var k in form.Keys) { var key = k.ToString(); var grade = 0; int.TryParse(form[key], out grade); if (key.StartsWith("grade_")) { var userId = key.Split('_')[1]; projectTask.GradeProjectTasks.Add(new GradeProjectTask { UserId = userId, Grade = grade, ProjectTaskId = projectTaskId }); } } projectTasksRepository.UpdateState(EntityState.Modified, projectTask); projectTasksRepository.SaveChanges(); return(RedirectToAction("Details", "ProjectTasks", new { Id = projectTaskId })); }