public void InsertOrUpdate(Player player) { if (player.Id == default(int)) { // New entity context.Players.Add(player); } else { // Existing entity context.Entry(player).State = EntityState.Modified; } }
public ActionResult Create(Player player) { if (ModelState.IsValid) { AddPostedFiles(player); playerRepository.InsertOrUpdate(player); playerRepository.Save(); return RedirectToAction("Index"); } else { ViewBag.PossibleTeams = teamRepository.All; ViewBag.States = this.stateRepository.GetStates(); return View(); } }
private void AddPostedFiles(Player player) { if (Request.Files.Count > 0) { // assume only 1 file posted HttpPostedFileBase hpf = Request.Files[0] as HttpPostedFileBase; if (hpf.ContentLength == 0) return; string newFileName = Guid.NewGuid().ToString() + Path.GetExtension(hpf.FileName); string serverFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "roster-images", newFileName); hpf.SaveAs(serverFileName); player.PhotoUri = "/roster-images/" + newFileName; } }