public async Task<ActionResult> AddAlbum(Album model) { model.Id = Guid.NewGuid().ToString(); var list = Request["filteredsongs"].Split(',').ToList(); model.NumberOfSongs = list == null ? 0 : list.Count; List<Song> songList = new List<Song>(); if(list.Count > 0) { foreach(var item in list) { if(string.IsNullOrEmpty(item) || item != "0") { var song = await db.Songs.Where(s => s.Id.Equals(item)).FirstOrDefaultAsync(); song.AlbumId = model.Id; songList.Add(song); } } } model.DateCreated = DateTime.Now; model.Songs = songList; var image = WebImage.GetImageFromRequest("file"); string path = ""; if(image != null) { path = Path.Combine(Server.MapPath("/Content/Image/AlbumImages/"), model.Id + "_" + Path.GetFileName(image.FileName)); image.Save(path); model.Thumbnail = "/Content/Image/AlbumImages/" + model.Id + "_" + Path.GetFileName(image.FileName); } else { model.Thumbnail = "/Content/Image/AlbumImages/album_default.png"; } if(ModelState.IsValid) { db.Albums.Add(model); await db.SaveChangesAsync(); } return RedirectToAction("ListAlbums"); }
public async Task<ActionResult> EditAlbum(Album model) { var list = Request["filteredsongs"].Split(',').ToList(); model.DateModified = DateTime.Now; model.NumberOfSongs = list == null ? 0 : list.Count; List<Song> songList = new List<Song>(); if(list.Count > 0) { foreach(var item in list) { if(string.IsNullOrEmpty(item) || item != "0") { var song = await db.Songs.Where(s => s.Id.Equals(item)).FirstOrDefaultAsync(); song.AlbumId = model.Id; songList.Add(song); } } } var img = WebImage.GetImageFromRequest("file"); if(img != null) { string imgPath = @"~/Content/Image/AlbumImages/" + model.Id + "_" + Path.GetFileName(img.FileName); img.Save(imgPath); model.Thumbnail = imgPath.Replace("~", ""); } else { model.Thumbnail = "/Content/Image/AlbumImages/album_default.png"; } if(ModelState.IsValid) { db.Entry(model).State = EntityState.Modified; await db.SaveChangesAsync(); return RedirectToAction("ListAlbums"); } return View("Error"); }
public async Task<ActionResult> AddAlbum() { var model = new Album(); model.Publishers = await db.Publishers.ToListAsync(); model.Performers = await db.Performers.ToListAsync(); return View(model); }