public IActionResult AddPlayer(PlayerAttributeViewModel player) { if (_context.Players.Any(p => p.PlayerName == player.PlayerName && p.Position == player.Position)) { //Player name and position already exists //since you're checking the combination of the two //you can still use ivalidatableobject in the future when you get back to this ModelState.AddModelError(nameof(Player.PlayerName), "Player name already exists"); } if (ModelState.IsValid) { var toCreate = new Player { PlayerName = player.PlayerName, Position = player.Position, Age = player.Age, Height = player.Height, Weight = player.Weight, Development = player.Development, OVR = player.OVR, SPD = player.SPD, ACC = player.ACC, STR = player.STR, AGI = player.AGI, ELU = player.ELU, BCV = player.BCV, CAR = player.CAR, JKM = player.JKM, SPM = player.SPM, SFA = player.SFA, TRK = player.TRK, CTH = player.CTH, CIT = player.CIT, SPC = player.SPC, RTE = player.RTE, RLS = player.RLS, JMP = player.JMP, THP = player.THP, SAC = player.SAC, MAC = player.MAC, DAC = player.DAC, RUN = player.RUN, PAC = player.PAC, RBK = player.RBK, PBK = player.PBK, IBL = player.IBL, TAK = player.TAK, POW = player.POW, BSH = player.BSH, FMV = player.FMV, PMV = player.PMV, MCV = player.MCV, ZCV = player.ZCV, PRS = player.PRS, PRC = player.PRC, PUR = player.PUR }; //it's usually a best practice to name your dbsets plural. Note: I mean dbset, not model. The model should be named Player, dbset: Players _context.Players.Add(toCreate); _context.SaveChanges(); //redirecttoaction causes you to lose any modelstate validation errors. you just need to return the view return(RedirectToAction("Position", new { position = player.Position })); //return Json(toCreate); } return(View()); }
//[HttpPost("AddPlayer")] public IActionResult AddPlayer(CreateViewModel player) { if (_context.Player.Any(p => p.PlayerName == player.PlayerName)) { if (_context.Player.Any(p => p.Position == player.Position)) { ModelState.AddModelError(nameof(Player.PlayerName), "Player name already exists"); } } if (ModelState.IsValid) { Player toCreate = new Player() { PlayerName = player.PlayerName, Position = player.Position, Age = player.Age, Height = player.Height, Weight = player.Weight, Development = player.Development, OVR = player.OVR, SPD = player.SPD, ACC = player.ACC, STR = player.STR, AGI = player.AGI, ELU = player.ELU, BCV = player.BCV, CAR = player.CAR, JKM = player.JKM, SPM = player.SPM, SFA = player.SFA, TRK = player.TRK, CTH = player.CTH, CIT = player.CIT, SPC = player.SPC, RTE = player.RTE, RLS = player.RLS, JMP = player.JMP, THP = player.THP, SAC = player.SAC, MAC = player.MAC, DAC = player.DAC, RUN = player.RUN, PAC = player.PAC, RBK = player.RBK, PBK = player.PBK, IBL = player.IBL, TAK = player.TAK, POW = player.POW, BSH = player.BSH, FMV = player.FMV, PMV = player.PMV, MCV = player.MCV, ZCV = player.ZCV, PRS = player.PRS, PRC = player.PRC, PUR = player.PUR }; _context.Player.Add(toCreate); _context.SaveChanges(); return(RedirectToAction("AddPlayer")); //return Json(toCreate); } return(View("Index")); }