public ActionResult Edit(int?id, PlaylistEditTrackDetails newItem) { // Validate the input if (!ModelState.IsValid) { // Our "version 1" approach is to display the "edit form" again return(RedirectToAction("edit", new { id = newItem.PlaylistId })); } if (id.GetValueOrDefault() != newItem.PlaylistId) { // This appears to be data tampering, so redirect the user away return(RedirectToAction("index")); } // Attempt to do the update var editedItem = m.PlaylistEditTracks(newItem); if (editedItem == null) { // There was a problem updating the object // Our "version 1" approach is to display the "edit form" again return(RedirectToAction("Edit")); } else { // Show the details view, which will have the updated data return(RedirectToAction("Details", new { id = newItem.PlaylistId })); } }
public PlaylistEditTrackDetails PlaylistEditTracks(PlaylistEditTrackDetails newItem) { var o = ds.Playlists.Include("Tracks").SingleOrDefault(p => p.PlaylistId == newItem.PlaylistId); if (o == null) { // Problem - object was not found, so return return(null); } else { // Update the object with the incoming values // First, clear out the existing collection o.Tracks.Clear(); // Then, go through the incoming items // For each one, add to the fetched object's collection foreach (var item in newItem.TrackIds) { var a = ds.Tracks.Find(item); o.Tracks.Add(a); } // Save changes ds.SaveChanges(); return(mapper.Map <Playlist, PlaylistEditTrackDetails>(o)); } }