Beispiel #1
0
        public async Task <IActionResult> Create([Bind("Id,Name,ReleaseDate,Genre,PublisherId,Picture")] Album album)
        {
            if (ModelState.IsValid)
            {
                _context.Add(album);

                if (Request.Form.Files.Count > 0)
                {
                    IFormFile file = Request.Form.Files.FirstOrDefault();
                    using var dataStream = new MemoryStream();
                    await file.CopyToAsync(dataStream);

                    album.Picture = dataStream.ToArray();
                }
                await _context.SaveChangesAsync();

                foreach (string id in Request.Form["Performers"])
                {
                    PerformerAlbum performerAlbum = new PerformerAlbum()
                    {
                        AlbumId     = album.Id,
                        PerformerId = int.Parse(id)
                    };

                    _context.Add(performerAlbum);
                }

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(album));
        }
Beispiel #2
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name,ReleaseDate,Genre,PublisherId,Picture")] Album album)
        {
            if (id != album.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (Request.Form.Files.Count > 0)
                    {
                        IFormFile file = Request.Form.Files.FirstOrDefault();
                        using var dataStream = new MemoryStream();
                        await file.CopyToAsync(dataStream);

                        album.Picture = dataStream.ToArray();
                    }

                    album = await _context.Albums.Include(album => album.PerformerAlbums).AsNoTracking().FirstOrDefaultAsync(album => album.Id == id);

                    foreach (var performerAlbum in album.PerformerAlbums)
                    {
                        _context.PerformerAlbums.Remove(performerAlbum);
                    }


                    if (Request.Form.Files.Count > 0)
                    {
                        IFormFile file = Request.Form.Files.FirstOrDefault();
                        using var dataStream = new MemoryStream();
                        await file.CopyToAsync(dataStream);

                        album.Picture = dataStream.ToArray();
                    }

                    _context.Update(album);
                    await _context.SaveChangesAsync();

                    foreach (string performerId in Request.Form["Performers"])
                    {
                        PerformerAlbum performerAlbum = new PerformerAlbum()
                        {
                            AlbumId     = album.Id,
                            PerformerId = int.Parse(performerId)
                        };

                        _context.Add(performerAlbum);
                    }
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AlbumExists(album.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(album));
        }