[HttpPost] //form post request from ApplyForTrip public async Task <IActionResult> SubmitTripApplication(TripApplyModel model, List <string> responses) { if (ModelState.IsValid) { Application app = new Application(); AppUser usr = await GetCurrentUserAsync(); app.StudentId = usr.Id; app.TripId = model.TripId; app.ApplicationDate = DateTime.Now; app.Gpa = model.Gpa; app.Rationale = model.Rationale; app.AppStatusId = model.AppStatusId; app.AcademicStatusId = 1; // hard-coding for now app.ModifiedDate = DateTime.Now; //save to db to create appId DbContext.Application.Add(app); DbContext.SaveChanges(); //get TripQuestionId from TripId IQueryable <TripQuestion> tripQuestions = DbContext.TripQuestion.Where(tq => tq.TripId == model.TripId); int i = 0; foreach (var tq in tripQuestions) { TripQuestionResponse tqr = new TripQuestionResponse(); tqr.Response = responses[i]; tqr.ApplicationId = app.ApplicationId; tqr.TripQuestionID = tq.TripQuestionID; DbContext.TripQuestionResponse.Add(tqr); i++; } //tqr.Response = model.Response; DbContext.SaveChanges(); return(RedirectToAction("Index")); } return(View()); }
//view trip by ID public IActionResult TripDetail(int id) { //find user var userId = User.FindFirst(ClaimTypes.NameIdentifier).Value; AppUser user = userManager.Users.FirstOrDefault(u => u.Id == userId); IQueryable <TripQuestion> questions = DbContext.TripQuestion.Where(tp => tp.TripId == id); ViewBag.Questions = questions; //get user's student info for modified date in viewbag var student = DbContext.Student.FirstOrDefault(s => s.StudentId == user.Id); ViewBag.ModDate = student.ModifiedDate; ViewBag.DOB = student.BirthDate; //viewbag for timespan TimeSpan timeSpan = DateTime.Now.Subtract(student.ModifiedDate); ViewBag.TimeSpan = Math.Ceiling(timeSpan.TotalDays); //find the corresponding trip in the DB Trip trip = tripContext.Trips.FirstOrDefault(t => t.TripId == id); //then course trip.Course = tripContext.Courses.FirstOrDefault(c => c.CourseId == trip.CourseId); //then destination trip.Destination = tripContext.Destinations.FirstOrDefault(d => d.DestinationId == trip.DestinationId); //return View(trip); TripApplyModel tam = new TripApplyModel(); tam.Trip = trip; return(View(tam)); }