Esempio n. 1
0
        // GET: Races/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var race = await _context.Races
                       .Include(r => r.CandidateRaces)
                       .FirstOrDefaultAsync(r => r.RaceId == id);

            RaceViewModel model = new RaceViewModel
            {
                Race       = race,
                Candidates = new SelectList(_context.Candidates
                                            .Where(c => c.ElectionId == _managedElectionID)
                                            .OrderBy(c => c.Name),
                                            "CandidateId", "Name")
            };

            if (race == null)
            {
                return(NotFound());
            }

            return(View(model));
        }
Esempio n. 2
0
        public async Task <IActionResult> Edit(int id, RaceViewModel model)
        {
            if (id != model.RaceId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var race = _context.Races.Find(id);
                    race.ElectionId   = model.ElectionId;
                    race.PositionName = model.PositionName;
                    race.NumberNeeded = model.NumberNeeded;
                    _context.Update(race);

                    var crs = _context.CandidateRaces.Where(cr => cr.RaceId == id).ToList();
                    _context.RemoveRange(crs);

                    foreach (var cr in model.RaceCandidatesIds)
                    {
                        _context.Add(new CandidateRace
                        {
                            CandidateId  = cr,
                            RaceId       = id,
                            PositionName = race.PositionName
                        });
                    }

                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RaceExists(model.RaceId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["Elections"] = new SelectList(_context.Elections, "ElectionId", "Name", model.ElectionId);
            return(View(model));
        }
Esempio n. 3
0
        public async Task <IActionResult> Edit(int id, RaceViewModel model)
        {
            if (id != model.Race.RaceId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(model.Race);

                    var crs = _context.CandidateRaces.Where(cr => cr.RaceId == id).ToList();
                    _context.RemoveRange(crs);

                    if (model.CandidateIds != null)
                    {
                        foreach (string cr in model.CandidateIds)
                        {
                            _context.Add(new CandidateRace
                            {
                                CandidateId = int.Parse(cr),
                                RaceId      = id,
                            });
                        }
                    }

                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RaceExists(model.Race.RaceId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }

            return(View(model));
        }
Esempio n. 4
0
        // GET: Races/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var race = await _context.Races.FindAsync(id);

            var crs = await _context.CandidateRaces
                      .Where(cr => cr.RaceId == id)
                      .ToListAsync();

            List <int> raceCandidateIds = new List <int>();

            foreach (var cr in crs)
            {
                raceCandidateIds.Add(cr.CandidateId);
            }

            RaceViewModel model = new RaceViewModel
            {
                ElectionId        = race.ElectionId,
                RaceId            = race.RaceId,
                PositionName      = race.PositionName,
                NumberNeeded      = race.NumberNeeded,
                RaceCandidatesIds = raceCandidateIds
            };

            if (race == null)
            {
                return(NotFound());
            }
            ViewData["Elections"]  = new SelectList(_context.Elections, "ElectionId", "Name");
            ViewData["Candidates"] = new SelectList(_context.Candidates.Where(c => c.ElectionId == State.currentElection), "CandidateId", "LastName");
            return(View(model));
        }