示例#1
0
        public async Task <IActionResult> Edit(SongsEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                var song = _dbContext.Songs.Find(model.Id);

                song.Name            = model.Name;
                song.AlbumId         = model.AlbumId;
                song.PerformerId     = _dbContext.Albums.Find(model.AlbumId).PerformerId;
                song.DurationSeconds = (model.DurationMinutes * 60 + model.DurationSeconds);

                try
                {
                    _dbContext.Songs.Update(song);
                    await _dbContext.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!_dbContext.Songs.Any(e => e.Id == song.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                return(Redirect("/Song"));
            }

            CreateSelectListForAlbums(model.AlbumId);

            return(View(model));
        }
示例#2
0
        public async Task <IActionResult> Edit(string id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var song = await _dbContext.Songs.FindAsync(id);

            if (song == null)
            {
                return(NotFound());
            }

            var model = new SongsEditViewModel()
            {
                Id              = song.Id,
                Name            = song.Name,
                DurationMinutes = (song.DurationSeconds / 60),
                DurationSeconds = (song.DurationSeconds % 60)
            };

            CreateSelectListForAlbums(model.AlbumId);

            return(View(model));
        }