I have two Model classes like so:
Program:
public class Program
{
public int ProgramId { get; set; }
public string Name { get; set; }
public virtual Playlist chosenPlaylist { get; set; }
public virtual IList<Playlist> Playlists { get; set; }
}
public class Playlist
{
public int PlaylistId { get; set; }
public string Name { get; set; }
public int NumberVotes { get; set; }
public virtual IList<Song> Songs { get; set; }
}
In my Edit Program View, I want to update the chosenPlaylist so I can allow the user to select none or one of the Program's Playlists.
For example:
Program 1:
- Playlist 1
- Playlist 2
Chosen Playlist: Playlist 1
So the user can then edit and select None (so no playlist), 1 (won't change anything) or 2 and that gets saved to the database.
I've tried to create a dropdownlist in my Controller but it won't update.
Here's what I have in both my GET and POST Edit ActionResults:
ViewBag.chosenId = new SelectList(program.Playlists, "PlaylistId",
"Name", program.chosenPlaylist.PlaylistId);
And in my View:
@Html.DropDownList("PlaylistId", (SelectList)ViewBag.chosenId)
This displays the list fine and pre-selects the chosen Playlist, if there is one (if not, I'll write code for it to default to the first). If there aren't playlists in a Program, that's easy to control.
However, problems:
- Doesn't update my model. If Playlist 2 is the chosen one, for example, and I choose P1, it continues to display P2 after the POST event.
- I want to include an option in the dropdownlist for it not to pick any value (so, place a NULL in that field). Is that possible?
There are no errors thrown, everything seems to work except for the most important part - updating the database.