public IActionResult EditSong(IFormCollection formCollection, string songName, string fileName, int id) { MusicTrack song = musicRepo.GetMusicTrackById(id); var collection = formCollection; GenreTag genreTag = musicRepo.GetGenreTagFromDataBase(collection["genre"]); song.Name = songName; song.Genre = genreTag; song.FileName = fileName; musicRepo.DeleteMusicTrackMoodTags(song); musicRepo.DeleteMusicTrackInstrumentTags(song); song.Moods = new List <MoodTag>(); song.Instruments = new List <InstrumentTag>(); song.MusicTrackMoodTags = new List <MusicTrackMoodTag>(); song.MusicTrackInstrumentTags = new List <MusicTrackInstrumentTag>(); for (int i = 0; i <= musicRepo.MoodList.Count; i++) { string mood = "mood" + i.ToString(); string value = collection[mood]; if (value != null) { MoodTag moodTag = musicRepo.GetMoodTagFromDatabase(value); song.Moods.Add(moodTag); var m = new MusicTrackMoodTag(); m.MusicTrack = song; m.MoodTag = moodTag; song.MusicTrackMoodTags.Add(m); } } for (int i = 0; i <= musicRepo.InstrumentList.Count; i++) { string instrument = "instrument" + i.ToString(); string value = collection[instrument]; if (value != null) { InstrumentTag instrumentTag = musicRepo.GetInstrumentTagFromDatabase(value); song.Instruments.Add(instrumentTag); var m = new MusicTrackInstrumentTag(); m.MusicTrack = song; m.InstrumentTag = instrumentTag; song.MusicTrackInstrumentTags.Add(m); } } musicRepo.MusicTracks.Add(song); musicRepo.UpdateSongInDatabase(song); return(RedirectToRoute("AdminManageSongs")); }