// GET: Appointments/Create public async Task <IActionResult> Create() { ViewData["UserId"] = new SelectList(_context.ApplicationUsers, "Id", "Id"); var ViewModel = new AppointmentWithSymptomListViewModel(); ApplicationDbContext applicationDbContext = _context; var activeUser = await GetCurrentUserAsync(); ViewModel.Symptoms = await _context.Symptoms.Where(u => u.UserId == activeUser.Id).ToListAsync(); // ViewModel.Symptoms = applicationDbContext.Symptoms.ToList(); return(View(ViewModel)); }
public async Task <IActionResult> Create(AppointmentWithSymptomListViewModel AppointmentViewModel) { ModelState.Remove("User"); ModelState.Remove("UserId"); ModelState.Remove("Appointment.User"); ModelState.Remove("Appointment.UserId"); //ModelState.Remove("AppointmentSymptom.appointment.User"); //ModelState.Remove("AppointmentSymptom.appointment.UserId"); //ApplicationUser user = await GetCurrentUserAsync(); if (ModelState.IsValid) { var user = await _userManager.GetUserAsync(HttpContext.User); AppointmentViewModel.UserId = user.Id; AppointmentViewModel.Appointment.UserId = user.Id; _context.Add(AppointmentViewModel.Appointment); foreach (int symptomId in AppointmentViewModel.SelectedSymptomIds) { AppointmentSymptom newAS = new AppointmentSymptom() { AppointmentID = AppointmentViewModel.Appointment.AppointmentID, SymptomID = symptomId, UserId = user.Id }; _context.Add(newAS); } // _context.Add(Appointment); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["UserId"] = new SelectList(_context.ApplicationUsers, "Id", "Id", AppointmentViewModel.UserId); return(View(AppointmentViewModel)); }
// GET: Appointments/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var ViewModel = new AppointmentWithSymptomListViewModel(); var activeUser = await GetCurrentUserAsync(); ViewModel.Appointment = await _context.Appointments.FindAsync(id); ViewModel.Symptoms = await _context.Symptoms.Where(u => u.UserId == activeUser.Id).ToListAsync(); ViewModel.SelectedSymptomIds = await _context.AppointmentSymptoms.Where(u => u.AppointmentID == id).Select(u => u.SymptomID).ToListAsync(); if (ViewModel.Appointment == null) { return(NotFound()); } //ViewData["UserId"] = new SelectList(_context.ApplicationUsers, "Id", "Id", ViewModel.Appointment.UserId); return(View(ViewModel)); }
public async Task <IActionResult> Edit(int id, AppointmentWithSymptomListViewModel AVM) { if (id != AVM.Appointment.AppointmentID) { return(NotFound()); } ModelState.Remove("User"); ModelState.Remove("UserId"); ModelState.Remove("Appointment.User"); ModelState.Remove("Appointment.UserId"); // var user = await GetCurrentUserAsync(); //AVM.UserId = user.Id; if (ModelState.IsValid) { try { var user = await GetCurrentUserAsync(); AVM.UserId = user.Id; /*foreach (int symptomId in AVM.SelectedSymptomIds) * //foreach (AppointmentSymptom SymptomID in _context.AppointmentSymptoms) * { * * AppointmentSymptom appointmentSymptomToRemove = await _context.AppointmentSymptoms.SingleOrDefaultAsync(op => op.AppointmentID == id); * * // Delete AppointmentSymptom * _context.AppointmentSymptoms.Remove(appointmentSymptomToRemove); * }*/ var appointmentSymptom = await _context.AppointmentSymptoms .Where(AS => AS.AppointmentID == id).ToListAsync(); foreach (AppointmentSymptom AS in appointmentSymptom) { _context.AppointmentSymptoms.Remove(AS); } await _context.SaveChangesAsync(); AVM.Appointment.UserId = user.Id; _context.Update(AVM.Appointment); /* foreach (Symptom S in AVM.Symptoms) * { * _context.Update(S); * }*/ foreach (int symptomId in AVM.SelectedSymptomIds) { AppointmentSymptom UpdateAS = new AppointmentSymptom() { AppointmentID = AVM.Appointment.AppointmentID, SymptomID = symptomId, UserId = user.Id }; _context.Update(UpdateAS); } await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AppointmentExists(AVM.Appointment.AppointmentID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["UserId"] = new SelectList(_context.ApplicationUsers, "Id", "Id", AVM.Appointment.UserId); return(View(AVM)); }