public async Task <IActionResult> Create([Bind("Id,Name")] Ganre ganre) { if (!IsDuplicate(ganre)) { if (ModelState.IsValid) { _context.Add(ganre); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(ganre)); } else { ModelState.AddModelError("Name", "Такий жанр уже існує"); } return(View(ganre)); }
public async Task <IActionResult> Create([Bind("Id,Name")] Category category) { if (!IsDuplicate(category)) { ModelState.AddModelError("Name", "Така категорія уже існує"); if (ModelState.IsValid) { _context.Add(category); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(category)); } else { ModelState.AddModelError("Name", "Така категорія уже існує"); } return(View(category)); }
public async Task <IActionResult> Create([Bind("Id,Name,CategoryId,Year,Info")] Film film, int firstId, string?retController) { FillSelectLists(firstId); FillReturnPath(retController); var ganresId = Request.Form["ganres"]; var actorsId = Request.Form["actors"]; List <int> gIds = new List <int>(); foreach (var g in ganresId) { gIds.Add(int.Parse(g)); } List <int> aIds = new List <int>(); foreach (var a in actorsId) { aIds.Add(int.Parse(a)); } if (!IsDuplicate(film, gIds, aIds, 0)) { if (ModelState.IsValid) { _context.Add(film); foreach (var gId in gIds) { var ganre = _context.Ganres.Where(g => g.Id == gId).FirstOrDefault(); FilmGanreRelationship fgr = new FilmGanreRelationship(); fgr.Film = film; fgr.GanreId = gId; fgr.Ganre = ganre; ganre.FilmGanreRelationships.Add(fgr); film.FilmGanreRelationships.Add(fgr); _context.FilmGanreRelationships.Add(fgr); } foreach (var aId in aIds) { var actor = _context.Actors.Where(a => a.Id == aId).FirstOrDefault(); FilmActorRelationship far = new FilmActorRelationship(); far.Film = film; far.ActorId = aId; far.Actor = actor; actor.FilmActorRelationships.Add(far); film.FilmActorRelationships.Add(far); _context.FilmActorRelationships.Add(far); } await _context.SaveChangesAsync(); return(RedirectToAction("Index", "Films", new { firstId = firstId, retController = retController })); } return(View(film)); } else { ModelState.AddModelError("", "Такий фільм уже існує"); } return(View(film)); }