public async Task <IActionResult> Edit(int id, [Bind("Id,ArtistId,GenreId")] ArtistsGenres artistsGenres) { if (id != artistsGenres.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(artistsGenres); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ArtistsGenresExists(artistsGenres.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["ArtistId"] = new SelectList(_context.Artists, "Id", "Name", artistsGenres.ArtistId); ViewData["GenreId"] = new SelectList(_context.Genres, "Id", "Name", artistsGenres.GenreId); return(View(artistsGenres)); }
public async Task <IActionResult> Create(int artistId, [Bind("Id,GenreId")] ArtistsGenres artistsGenres) { artistsGenres.ArtistId = artistId; if (ModelState.IsValid && !_context.ArtistsGenres.Any(ag => ag.GenreId == artistsGenres.GenreId && ag.ArtistId == artistId)) { _context.Add(artistsGenres); await _context.SaveChangesAsync(); return(RedirectToAction("Index", "ArtistsGenres", new { id = artistId, name = _context.Artists.Where(a => a.Id == artistId).FirstOrDefault().Name })); } //ViewData["ArtistId"] = new SelectList(_context.Artists, "Id", "Name", artistsGenres.ArtistId); ViewData["GenreId"] = new SelectList(_context.Genres, "Id", "Name", artistsGenres.GenreId); return(RedirectToAction("Index", "ArtistsGenres", new { id = artistId, name = _context.Artists.Where(a => a.Id == artistId).FirstOrDefault().Name })); //return View(artistsGenres); }
public async Task <IActionResult> Import(IFormFile fileExcel) { if (ModelState.IsValid) { if (fileExcel != null) { using (var stream = new FileStream(fileExcel.FileName, FileMode.Create)) { await fileExcel.CopyToAsync(stream); using (XLWorkbook workbook = new XLWorkbook(stream, XLEventTracking.Disabled)) { foreach (IXLWorksheet worksheet in workbook.Worksheets) { Countries country; var c1 = (from c2 in _context.Countries where c2.Name.Contains(worksheet.Name) select c2).ToList(); if (c1.Count > 0) { country = c1[0]; } else { country = new Countries(); country.Name = worksheet.Name; _context.Countries.Add(country); } foreach (IXLRow row in worksheet.RowsUsed().Skip(1)) { try { Artists artist = new Artists(); artist.Name = row.Cell(1).Value.ToString(); artist.Description = row.Cell(6).Value.ToString(); artist.Country = country; _context.Artists.Add(artist); for (int i = 2; i <= 5; i++) { if (row.Cell(i).Value.ToString().Length > 0) { Genres genre = null; foreach (Genres g in _context.Genres) { string nameNoSpaces = g.Name; nameNoSpaces.Replace(" ", ""); string cellName = row.Cell(i).Value.ToString(); cellName.Replace(" ", ""); if (nameNoSpaces == cellName) { genre = g; break; } } if (genre == null) { genre = new Genres(); genre.Name = row.Cell(i).Value.ToString(); _context.Add(genre); } ArtistsGenres ag = new ArtistsGenres(); ag.Artist = artist; ag.Genre = genre; _context.ArtistsGenres.Add(ag); } } } catch { return(RedirectToAction("Error", "Home")); } } } } } } await _context.SaveChangesAsync(); } return(RedirectToAction(nameof(Index))); }