public async Task <IActionResult> EditSong(int id) { ViewData["IdSinger"] = new SelectList(_context.Singer, "Id", "Name"); ViewData["ListGenre"] = new SelectList(SongModel.GetAllGerne()); var song = await _context.Song.FindAsync(id); return(View(song)); }
public async Task <IActionResult> Create([Bind("Name,ReleaseDate,URLImg,URLMusic,Genre,Lyric,IdAlbum")] SongModel song, IFormFile ful, IFormFile fulMusic, List <int> IdSinger) { if (ModelState.IsValid) { //Admin tạo bài hát thì luôn được cho phép hiển thị lên frontend song.Accept = true; song.CountLike = 0; if (song.IdAlbum == 0) { song.IdAlbum = null; } //Khởi tạo số view cho bài hát mới là 0 song.CountView = 0; song.NameUnsigned = RemoveUnicode(song.Name); _context.Add(song); await _context.SaveChangesAsync(); if (IdSinger != null) { foreach (var item in IdSinger) { var singer = await _context.Singer.FindAsync(item); if (item != IdSinger.LastOrDefault()) { song.NameSinger += singer.Name + ", "; } else { song.NameSinger += singer.Name; } } } song.NameUnsignedSinger = RemoveUnicode(song.NameSinger.Trim()); _context.Update(song); await _context.SaveChangesAsync(); if (ful != null) { var path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot/img/song", song.Id + "." + ful.FileName.Split(".")[ful.FileName.Split(".").Length - 1]); using (var stream = new FileStream(path, FileMode.Create)) { await ful.CopyToAsync(stream); } song.URLImg = song.Id + "." + ful.FileName.Split(".")[ful.FileName.Split(".").Length - 1]; _context.Update(song); await _context.SaveChangesAsync(); } if (fulMusic != null) { //Bỏ dấu string NameMusic = RemoveUnicode(song.Name); //Bỏ khoảng cách NameMusic = NameMusic.Replace(" ", String.Empty); var path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot/audio", NameMusic + "." + fulMusic.FileName.Split(".")[fulMusic.FileName.Split(".").Length - 1]); using (var stream = new FileStream(path, FileMode.Create)) { await fulMusic.CopyToAsync(stream); } song.URLMusic = NameMusic + "." + fulMusic.FileName.Split(".")[fulMusic.FileName.Split(".").Length - 1]; _context.Update(song); await _context.SaveChangesAsync(); } if (IdSinger != null) { foreach (var item in IdSinger) { SingerOfSong singerOfSong = new SingerOfSong(); singerOfSong.IdSinger = item; singerOfSong.IdSong = song.Id; _context.Add(singerOfSong); await _context.SaveChangesAsync(); } } return(RedirectToAction(nameof(Index))); } ViewData["IdSinger"] = new SelectList(_context.Singer, "Id", "Name"); ViewData["ListGenre"] = new SelectList(SongModel.GetAllGerne()); ViewBag.Alert = "Tạo mới bài hát không thành công, vui lòng thử lại"; return(View(song)); }
public IActionResult CreateSong() { ViewData["ListGenre"] = new SelectList(SongModel.GetAllGerne()); ViewData["IdSinger"] = new SelectList(_context.Singer, "Id", "Name"); return(View()); }