Beispiel #1
0
        public async Task <IActionResult> Edit(int id, TermPatientEditViewModel viewmodel)
        {
            if (id != viewmodel.Term.TermID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(viewmodel.Term);
                    await _context.SaveChangesAsync();

                    IEnumerable <int>        listPatients = viewmodel.SelectedPatients;
                    IQueryable <PatientTerm> toBeRemoved  = _context.PatientTerm.Where(s => !listPatients.Contains(s.PatientId) && s.TermId == id);
                    _context.PatientTerm.RemoveRange(toBeRemoved);
                    IEnumerable <int> existPatients = _context.PatientTerm.Where(s => listPatients.Contains(s.PatientId) && s.TermId == id).Select(s => s.PatientId);
                    IEnumerable <int> newPatients   = listPatients.Where(s => !existPatients.Contains(s));
                    foreach (int patientId in newPatients)
                    {
                        _context.PatientTerm.Add(new PatientTerm {
                            PatientId = patientId, TermId = id
                        });
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TermsExists(viewmodel.Term.TermID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["DoctorId"] = new SelectList(_context.Doctors, "DoctorId", "FullName", viewmodel.Term.DoctorId);
            return(View(viewmodel));
        }
Beispiel #2
0
        // GET: Terms/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var terms = _context.Terms.Where(m => m.TermID == id).Include(m => m.Patients).First();

            if (terms == null)
            {
                return(NotFound());
            }
            TermPatientEditViewModel viewmodel = new TermPatientEditViewModel
            {
                Term             = terms,
                PatientList      = new MultiSelectList(_context.Patients.OrderBy(s => s.FullName), "Id", "FullName"),
                SelectedPatients = terms.Patients.Select(sa => sa.PatientID)
            };

            ViewData["DoctorId"] = new SelectList(_context.Doctors, "DoctorId", "FullName", terms.DoctorId);
            return(View(viewmodel));
        }