Пример #1
0
        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 }));
        }