public IActionResult AddArtist(ArtistPostIndexModel model) { if (model == null) { RedirectToAction("ArtistInfo", new { id = model.Artist.Id }); } var artistModel = model.Artist; var genreModel = model.Genres; //add the artist - populate an Artist instance with values from the form var artist = new Artist { Id = artistModel.Id, //is 0 here ArtistName = artistModel.ArtistName, Bio = artistModel.Bio, YrFormed = artistModel.YrFormed, YrEnded = artistModel.YrEnded, HomeCountry = artistModel.HomeCountry, HomeTown = artistModel.HomeTown, isActive = artistModel.isActive }; _artist.Add(artist); //now add the genre list ... var genres = genreModel.Select (g => new GenreListingModel { Id = g.Id, Name = g.Name, isMarked = g.isMarked } ).ToList(); //... so loop through returned genres model to update table... foreach (var g in genres) { if (g.isMarked) { _artistGenre.Add(g.Id, artist.Id); } ; } //should go back to details page ... ArtistMod?? //temp!!! redirect to main page return(RedirectToAction("Index", "Main", new { genreId = 1, artistId = model.Artist.Id })); }
public IActionResult EditArtist(ArtistModIndexModel model) { if (model == null) { RedirectToAction("ArtistMod", new { id = model.Artist.Id }); } var artistModel = model.Artist; var genreModel = model.Genres; //var musicianModel = model.Musicians; //not editing musicians here - model will be passed but will be null //update the artist - populate an Artist instance with values from the form var artist = new Artist { Id = artistModel.Id, ArtistName = artistModel.ArtistName, Bio = artistModel.Bio, YrFormed = artistModel.YrFormed, YrEnded = artistModel.YrEnded, HomeCountry = artistModel.HomeCountry, HomeTown = artistModel.HomeTown, isActive = artistModel.isActive }; _artist.Update(artist); //now update the genre list ... var genres = genreModel.Select (g => new GenreListingModel { Id = g.Id, Name = g.Name, isMarked = g.isMarked } ).ToList(); //... so loop through returned genres model to update table... foreach (var g in genres) { if (_artistGenre.isMarked(g.Id, model.Artist.Id)) { //marked is true in DB, so now determine the appropriate action if (!g.isMarked) { //it has been unchecked, so remove the row in the DB _artistGenre.Delete(g.Id, model.Artist.Id); } // else - it is checked = NO ACTION REQD. } else { //marked is false in DB, so now determine the appropriate action if (g.isMarked) { //it has been checked, so add the row in the DB _artistGenre.Add(g.Id, model.Artist.Id); } // else - it is unchecked = NO ACTION REQD. } } @ViewBag.Message = "Saved"; //should we be handling genre checkbox updates via onclick="this.form.submit();" ?? return(RedirectToAction("Index", "ArtistMod", new { id = model.Artist.Id })); }