public ActionResult AddSongToList(int?id) { string uname = User.Identity.Name; var obj = db.Logins.Where(x => x.Username.Equals(uname)).FirstOrDefault(); int userID = obj.id; if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var pListViewModels = new PsongList { Sonk = db.Songs.Include(d => d.Playlistas).FirstOrDefault(d => d.id == id), }; if (pListViewModels.Sonk == null) { return(HttpNotFound()); } var allPlaylistasList = db.Playlistas.ToList(); pListViewModels.AllPlaylists = allPlaylistasList.Where(w => w.id_User == userID).Select(x => new SelectListItem { Text = x.id + " " + x.Name + " ", Value = x.id.ToString() }); return(View(pListViewModels)); }
public ActionResult AddSongToList(PsongList pListViewModels) { if (pListViewModels == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (ModelState.IsValid) { var update = db.Songs .Include(i => i.Playlistas) .FirstOrDefault(i => i.id == pListViewModels.Sonk.id); if (TryUpdateModel(update, "Playlist", new string[] { "id_User", "Name" })) { var song = db.Songs.Where(m => pListViewModels.SelectedPlaylists.Contains(m.id)).ToList(); var upSong = new HashSet <int>(pListViewModels.SelectedPlaylists); foreach (Playlista songE in db.Playlistas) { if (!upSong.Contains(songE.id)) { update.Playlistas.Remove(songE); } else { update.Playlistas.Add(songE); } } db.Entry(update).State = EntityState.Modified; db.SaveChanges(); } return(RedirectToAction("Index")); } return(View(pListViewModels)); }