Exemple #1
0
        // GET: Treenerid
        public async Task <IActionResult> Index(int?id, int?spordialaID)
        {
            var viewModel = new TreenerIndexData();

            viewModel.Treenerid = await _context.Treenerid
                                  .Include(i => i.AsutuseAssignment)
                                  .Include(i => i.SpordialaAssignments)
                                  .ThenInclude(i => i.Spordiala)
                                  .ThenInclude(i => i.Registreeringud)
                                  .ThenInclude(i => i.Sportlane)
                                  .Include(i => i.SpordialaAssignments)
                                  .ThenInclude(i => i.Spordiala)
                                  .ThenInclude(i => i.Osakond)
                                  .AsNoTracking()
                                  .OrderBy(i => i.Perekonnanimi)
                                  .ToListAsync();

            if (id != null)
            {
                ViewData["TreenerID"] = id.Value;
                Treener treener = viewModel.Treenerid.Where(
                    i => i.ID == id.Value).Single();
                viewModel.Spordialad = treener.SpordialaAssignments.Select(s => s.Spordiala);
            }

            if (spordialaID != null)
            {
                ViewData["SpordialaID"] = spordialaID.Value;
                var selectedCourse = viewModel.Spordialad.Where(x => x.SpordialaID == spordialaID).Single();
                await _context.Entry(selectedCourse).Collection(x => x.Registreeringud).LoadAsync();

                foreach (Registreering registreering in selectedCourse.Registreeringud)
                {
                    await _context.Entry(registreering).Reference(x => x.Sportlane).LoadAsync();
                }
                viewModel.Registreeringud = selectedCourse.Registreeringud;
            }

            return(View(viewModel));
        }
        public async Task <IActionResult> Edit(int?id, byte[] rowVersion)
        {
            if (id == null)
            {
                return(NotFound());
            }
            var osakondToUpdate = await _context.Osakonnad.Include(i => i.Administrator).FirstOrDefaultAsync(m => m.OsakondID == id);

            if (osakondToUpdate == null)
            {
                Osakond deletedOsakond = new Osakond();
                await TryUpdateModelAsync(deletedOsakond);

                ModelState.AddModelError(string.Empty, "Ei saa muudatusi salvestada. Osakond on teise kasutaja poolt kustutatud.");
                ViewData["TreenerID"] = new SelectList(_context.Treenerid, "ID", "Täisnimi", deletedOsakond.TreenerID);
                return(View(deletedOsakond));
            }
            _context.Entry(osakondToUpdate).Property("RowVersion").OriginalValue = rowVersion;

            if (await TryUpdateModelAsync <Osakond>(osakondToUpdate, "",
                                                    s => s.Nimi, s => s.AlgusKP, s => s.Eelarve, s => s.TreenerID))
            {
                try
                {
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    var exceptionEntry = ex.Entries.Single();
                    var clientValues   = (Osakond)exceptionEntry.Entity;
                    var databaseEntry  = exceptionEntry.GetDatabaseValues();
                    if (databaseEntry == null)
                    {
                        ModelState.AddModelError(string.Empty,
                                                 "Ei saa muudatusi salvestada. Osakond on teise kasutaja poolt kustutatud.");
                    }
                    else
                    {
                        var databaseValues = (Osakond)databaseEntry.ToObject();

                        if (databaseValues.Nimi != clientValues.Nimi)
                        {
                            ModelState.AddModelError("Nimi", $"Current value: {databaseValues.Nimi}");
                        }
                        if (databaseValues.Eelarve != clientValues.Eelarve)
                        {
                            ModelState.AddModelError("Eelarve", $"Current value: {databaseValues.Eelarve:c}");
                        }
                        if (databaseValues.AlgusKP != clientValues.AlgusKP)
                        {
                            ModelState.AddModelError("AlgusKP", $"Current value: {databaseValues.AlgusKP:d}");
                        }
                        if (databaseValues.TreenerID != clientValues.TreenerID)
                        {
                            Treener databaseInstructor = await _context.Treenerid.FirstOrDefaultAsync(i => i.ID == databaseValues.TreenerID);

                            ModelState.AddModelError("TreenerID", $"Current value: {databaseInstructor?.Täisnimi}");
                        }

                        ModelState.AddModelError(string.Empty, "The record you attempted to edit "
                                                 + "was modified by another user after you got the original value. The "
                                                 + "edit operation was canceled and the current values in the database "
                                                 + "have been displayed. If you still want to edit this record, click "
                                                 + "the Save button again. Otherwise click the Back to List hyperlink.");
                        osakondToUpdate.RowVersion = (byte[])databaseValues.RowVersion;
                        ModelState.Remove("RowVersion");
                    }
                }
            }
            ViewData["TreenerID"] = new SelectList(_context.Treenerid, "ID", "Täisnimi", osakondToUpdate.TreenerID);
            return(View(osakondToUpdate));
        }