public ActionResult Edit(CountriesEditModel countriesEditModel, int?id) { using (tennisDBEntities db = new tennisDBEntities()) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (Request.RequestType == "POST") { if (ModelState.IsValid) { country country = db.countries.Find(id); db.Entry(country).CurrentValues.SetValues(countriesEditModel.Country); db.Entry(country).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } } countriesEditModel.Country = db.countries.Find(id); if (countriesEditModel.Country == null) { return(HttpNotFound()); } } return(View(countriesEditModel)); }
public ActionResult Upload(HttpPostedFileBase file, PlayersUploadModel playersUploadModel, int?id) { using (tennisDBEntities db = new tennisDBEntities()) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } playersUploadModel.Player = db.players.Find(id.Value); if (playersUploadModel.Player == null) { return(HttpNotFound()); } if (playersUploadModel.Delete == true) { playersUploadModel.Player.photo = null; db.Entry(db.players.Find(id)).CurrentValues.SetValues(playersUploadModel.Player); db.Entry(db.players.Find(id)).State = EntityState.Modified; db.SaveChanges(); } if (file != null && file.ContentLength > 0) { Stream fs = file.InputStream; BinaryReader br = new BinaryReader(fs); byte[] bytes = br.ReadBytes((Int32)fs.Length); playersUploadModel.Player.photo = bytes; db.Entry(db.players.Find(id)).CurrentValues.SetValues(playersUploadModel.Player); db.Entry(db.players.Find(id)).State = EntityState.Modified; db.SaveChanges(); } } return(View(playersUploadModel)); }
public ActionResult Edit(PlayersEditModel playersEditModel, int?id) { using (tennisDBEntities db = new tennisDBEntities()) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (Request.RequestType == "POST") { if (ModelState.IsValid) { player player = db.players.Find(id); db.Entry(player).CurrentValues.SetValues(playersEditModel.Player); db.Entry(player).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Details", new { id })); } } playersEditModel.Player = db.players.Find(id.Value); if (playersEditModel.Player == null) { return(HttpNotFound()); } int Year = DateTime.Now.Year; List <int?> Years = new List <int?>() { null }; for (int i = 40; i > 0; i--) { Years.Add(Year); Year--; } playersEditModel.Years = new SelectList(Years); if (playersEditModel.Player.dob.HasValue) { playersEditModel.Dob = playersEditModel.Player.dob.Value.ToString("yyyy-MM-dd"); playersEditModel.Age = AgeFormatter.FormatAge(DateTime.Parse(playersEditModel.Dob)); } } return(View(playersEditModel)); }
public static void ResetRank(int year, string gender, tennisDBEntities db) { int newRank = 0; int ordinalRank = 0; int lastPoints = int.MaxValue; List <ranking> UpdatedRankings = db.rankings .Select(ranking => ranking) .Where(r => r.year.Value == year && r.player.gender == gender) .OrderByDescending(r => r.points) .ToList(); foreach (ranking Ranking in UpdatedRankings) { ordinalRank++; if (Ranking.points < lastPoints) { newRank = ordinalRank; } lastPoints = Ranking.points.Value; Ranking.rank = newRank; db.Entry(Ranking).State = EntityState.Modified; } }
public ActionResult Edit(RankingsEditModel rankingsEditModel, int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } rankingsEditModel.Player = db.players.Find(id); if (rankingsEditModel.Player == null) { return(HttpNotFound()); } int year = DateTime.Now.Year; for (int i = 2000; i <= year; year--) { rankingsEditModel.Years.Add(year); } if (Request.RequestType == "GET") { rankingsEditModel.Rankings = db.rankings.Select(ranking => ranking).Where(r => r.player_id == id).OrderByDescending(r => r.year).ToList(); foreach (ranking item in rankingsEditModel.Rankings) { rankingsEditModel.Delete.Add(false); } } if (Request.RequestType == "POST") { if (ModelState.IsValid) { List <int> ChangedYears = new List <int>(); for (int i = rankingsEditModel.Delete.Count - 1; i >= 0; i--) { if (rankingsEditModel.Delete[i] == true) { if (db.rankings.Find(rankingsEditModel.Rankings[i].id) != null) { db.rankings.Remove(db.rankings.Find(rankingsEditModel.Rankings[i].id)); } rankingsEditModel.Rankings.RemoveAt(i); } } foreach (ranking Ranking in rankingsEditModel.Rankings) { Ranking.player = db.players.Find(id); ranking NewRanking = new ranking(); if (Ranking.id > 0) { NewRanking = db.rankings.Find(Ranking.id); db.Entry(NewRanking).CurrentValues.SetValues(Ranking); db.Entry(NewRanking).State = EntityState.Modified; } if (Ranking.id == 0) { db.rankings.Add(Ranking); } ChangedYears.Add(Ranking.year.Value); } if (ChangedYears.Distinct().Count() == ChangedYears.Count()) { db.SaveChanges(); foreach (int Year in ChangedYears) { RankingsUpdate.ResetRank(Year, rankingsEditModel.Player.gender, db); } db.SaveChanges(); return(RedirectToAction("Details", "Players", new { id })); } else { var DuplicateYears = ChangedYears.GroupBy(y => y); var KeyList = ModelState.Keys.Select(k => k).Where(k => k.EndsWith(".year")); foreach (var y in DuplicateYears) { if (y.Count() > 1) { foreach (var k in KeyList) { if (ModelState[k].Value.AttemptedValue == y.Key.ToString()) { ModelState.AddModelError(k, "Maximum 1 ranking per year"); } } } } } } } if (rankingsEditModel.Delete.Count == rankingsEditModel.Rankings.Count) { ranking Ranking = new ranking(); Ranking.player_id = id.Value; rankingsEditModel.Rankings.Add(Ranking); } return(View(rankingsEditModel)); }