1

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.

3 Answers 3

3

Make sure there are no @Html.HiddenFor or similar rendering the same item.

Sign up to request clarification or add additional context in comments.

1 Comment

Cheers for this, caught me out bigtime!
2

There's 2 things you can do:

Change @Html.DropDownList("PlaylistId", (SelectList)ViewBag.chosenId) into @Html.DropDownList("chosenPlaylist.PlaylistId", (SelectList)ViewBag.chosenId)

Or use the Html.DropDownListFor(m => m.chosenPlaylist.PlaylistId, (SelectList)ViewBag.chosenId)).

4 Comments

None of these solutions seem to work for me. The first one throws up System.ArgumentNullException: Value cannot be null. Parameter name: items on the Controller and the second one won't even work in my Razor (no overload takes 1 arguments).
Sorry I had quickly written up from memory. The DropDownListFor takes more then 1 parameter, this will surely be the easiest way for what you wish to accomplish (since MVC will then look into your model and create the right names for the model to be rebound again on post) Regarding the argumentnullexception... What does your ViewBag contain? Possibly you're given the "selected item" to the "items" parameter. I can't verify code atm since I'm mobile.
Worked perfectly !! I wasted a day to find out why the edit template not work !! Thanks again IvanL !!
Worked, thanks! TIP: Do not forget the { get; set; } accessors in the view model or you will just get 0 as the chosenId value in the controller when the form is submitted.
0

When using:

@Html.DropDownList("PlaylistId", (SelectList)ViewBag.chosenId)

you have to rename the PlaylistId select list to something that is not the same as the propertyname that stores the selected Id, else it wont be marked as selected.

(which makes sense now that im typing it, you cant store the selected value into something that has the same as the select list)

Basically saying:

@Html.DropDownList("MySelectListId", (SelectList)ViewBag.chosenId)

will work.

You can look at the comments on this issue at codeplex for more information: http://aspnet.codeplex.com/workitem/4932

1 Comment

It doesn't give errors, but it still doesn't update my Model unfortunately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.