public async Task <IActionResult> PutTrainingClassStudent([FromRoute] int trainingClassId, [FromBody] TrainingClassParticipant trainingClassParticipant) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!TrainingClassParticipantExists(trainingClassId, trainingClassParticipant.MemberID)) { var trainingClassStudent = new TrainingClassStudent() { TrainingClassId = trainingClassId, TrainingClassStudentMemberId = trainingClassParticipant.MemberID, TrainingClassStudentHours = trainingClassParticipant.Hours, Created = DateTime.UtcNow }; try { _context.TrainingClassStudent.Add(trainingClassStudent); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TrainingClassStudentExists(trainingClassId, trainingClassParticipant.MemberID)) { return(NotFound()); } else { throw; } } } return(NoContent()); }
public ViewResult CreateOccurrence(Models.Training.TrainingClassInsert trainingClassInsert) { Models.DB.TrainingClass trainingClass = new TrainingClass(); //the class itself List <Models.Training.TrainingClassParticipant> students = new List <Models.Training.TrainingClassParticipant>(); //the students' participation in the above training class List <Models.Training.TrainingClassParticipant> instructors = new List <Models.Training.TrainingClassParticipant>(); //the instructors' participation in the above training class var x = HttpContext.Request.Form; var thanks = new Models.Training.TrainingClassInsertConfirmation(); Int32 i = 0; Int32 j = 0; //gross, but looping through the form data to find students/instructors/hours, not using binding for this foreach (var item in x) { if (item.Key == "member") { foreach (var y in item.Value) { //gets the memberid Models.Training.TrainingClassParticipant topi = new Models.Training.TrainingClassParticipant(); String maybememberid = y.ToString(); topi.MemberID = Int32.Parse(y.ToString()); //get the hours string maybeHours = x["hours"][i]; topi.Hours = Decimal.Parse(maybeHours); //keeping a counter so (above) we know how to find the hours by index. ie: for the third member, we look for the third set of hours. students.Add(topi); i = i + 1; } } if (item.Key == "instructor") { foreach (var y in item.Value) { //gets the memberid Models.Training.TrainingClassParticipant topi = new Models.Training.TrainingClassParticipant(); String maybememberid = y.ToString(); topi.MemberID = Int32.Parse(y.ToString()); //get the hours string maybeHours = x["ihours"][j]; topi.Hours = Decimal.Parse(maybeHours); //keeping a counter so (above) we know how to find the hours by index. ie: for the third member, we look for the third set of hours. instructors.Add(topi); j = j + 1; } } } trainingClass.Created = DateTime.UtcNow; trainingClass.TrainingDate = trainingClassInsert.TrainingDate; trainingClass.TrainingId = trainingClassInsert.TrainingID; trainingClass.TrainingClassStudent = new List <TrainingClassStudent>(); trainingClass.TrainingClassInstructor = new List <TrainingClassInstructor>(); //parse the form and grab the instructors and students, along with their hours objects to store foreach (var item in students) { Models.DB.TrainingClassStudent trainingClassStudent = new TrainingClassStudent(); trainingClassStudent.Created = DateTime.UtcNow; trainingClassStudent.TrainingClassStudentMemberId = item.MemberID; trainingClassStudent.TrainingClassStudentHours = item.Hours; trainingClass.TrainingClassStudent.Add(trainingClassStudent); } foreach (var item in instructors) { Models.DB.TrainingClassInstructor trainingClassInstructor = new TrainingClassInstructor(); trainingClassInstructor.Created = DateTime.UtcNow; trainingClassInstructor.TrainingClassInstructorMemberId = item.MemberID; trainingClassInstructor.TrainingClassStudentHours = item.Hours; trainingClass.TrainingClassInstructor.Add(trainingClassInstructor); } try { _context.Add(trainingClass); _context.SaveChanges(); } catch (Exception exc) { throw new Exception("Error saving new class (EF): " + exc.ToString()); } var hours = students.Sum(item => item.Hours).ToString(); var people = students.Count.ToString(); thanks.Message = "Great work, your team has added " + hours + " hours of training across " + people + " members."; return(View("TrainingOccurrenceInsertConfirmation", thanks)); }