/// <summary>
        /// Delete a Championship by id.
        /// </summary>
        /// <param name="id">Championship Id</param>
        public async Task <ActionResult> Delete(int?id)
        {
            if (id.HasValue)
            {
                try
                {
                    var championship = await _context.Championship.FindAsync(id.Value);

                    if (championship != null)
                    {
                        using (_context)
                        {
                            _context.Entry(championship).State = EntityState.Deleted;
                            await _context.SaveChangesAsync();
                        }

                        TempData["Message"] = "Championship deleted successfully!";
                    }
                }
                catch (Exception ex)
                {
                    TempData["Message"] = "Error: " + ex.Message;
                }

                return(RedirectToAction("Index", "Home"));
            }

            return(BadRequest());
        }
예제 #2
0
        public async Task <ActionResult> Delete(int?id)
        {
            if (id.HasValue)
            {
                try
                {
                    var team = await _context.Team.FindAsync(id.Value);

                    if (team != null)
                    {
                        _context.Entry(team).Collection(t => t.TeamChampionships).Load();

                        if (team.TeamChampionships.Count != 0)
                        {
                            throw new Exception("You can't delete a team that's already in a championship.");
                        }

                        using (_context)
                        {
                            _context.Entry(team).State = EntityState.Deleted;
                            await _context.SaveChangesAsync();
                        }

                        TempData["Message"] = "Team " + team.Name + " deleted successfully!";
                    }
                }
                catch (Exception ex)
                {
                    TempData["Message"] = "Error: " + ex.Message;
                }

                return(RedirectToAction("Index", "Home"));
            }

            return(BadRequest());
        }