public async Task <IActionResult> Create(CreateBandArtistViewModel viewModel, int id) { if (ModelState.IsValid) { List <Artist> artists = _context.Artists.ToList(); List <BandArtist> bandArtists = _context.BandArtists.Where(x => x.BandID == id).ToList(); if (artists.Contains(viewModel.BandArtist.Artist)) { int existsID = _context.Artists.FirstOrDefault(x => x.FirstName == viewModel.BandArtist.Artist.FirstName && x.LastName == viewModel.BandArtist.Artist.LastName).ArtistID; viewModel.BandArtist.Artist = _context.Artists.FirstOrDefault(x => x.ArtistID == existsID); } viewModel.BandArtist.BandID = id; if (!bandArtists.Contains(viewModel.BandArtist)) { _context.Add(viewModel.BandArtist); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Details), "Band", new { id })); } else { return(RedirectToAction(nameof(Create), "BandArtist", new { id })); } } return(View(viewModel)); }
public async Task <IActionResult> Edit(int id, int artistID, int bandID, CreateBandArtistViewModel viewModel) { if (id != viewModel.BandArtist.BandArtistID) { return(NotFound()); } viewModel.BandArtist.ArtistID = artistID; viewModel.BandArtist.BandID = bandID; if (ModelState.IsValid) { try { _context.Update(viewModel.BandArtist); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BandArtistExists(viewModel.BandArtist.BandArtistID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Details), "Band", new { id = bandID })); } return(View(viewModel)); }
public IActionResult Create(int id) { CreateBandArtistViewModel viewModel = new CreateBandArtistViewModel { Band = _context.Bands.FirstOrDefault(x => x.BandID == id), Artists = new SelectList(_context.Artists, "ArtistID", "FullName"), Bands = new SelectList(_context.Bands, "BandID", "Name"), Roles = new SelectList(_context.ArtistRoles, "RoleID", "Name") }; return(View(viewModel)); }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } CreateBandArtistViewModel viewModel = new CreateBandArtistViewModel(); viewModel.BandArtist = await _context.BandArtists.FindAsync(id); if (viewModel.BandArtist == null) { return(NotFound()); } viewModel.Bands = new SelectList(_context.Bands, "BandID", "Name"); viewModel.Roles = new SelectList(_context.ArtistRoles, "RoleID", "Name", viewModel.BandArtist.RoleID); viewModel.Artists = new SelectList(_context.Artists, "ArtistID", "FirstName", viewModel.BandArtist.ArtistID); return(View(viewModel)); }