Libor Zapletal
Libor Zapletal

Reputation: 14092

ASP.NET MVC3 dropdownlist not get selected item

I have this in my edit controller:

playerView.Users = new SelectList(repo.GetUsers(), "UserID", "UserName", player.User.UserID.ToString());

and this is how looks my dropdownlist:

@Html.DropDownListFor(x => x.SelectedUserID, Model.Users)
@Html.ValidationMessageFor(x => x.SelectedUserID)

But I can´t get that selected user to my view (I always get first item in dropdownlist). In player.User is User with id, name, ... I tried UserID with or without ToString, UserName or just User but nothing helps. Thanks

Upvotes: 0

Views: 538

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Try like this:

...
playerView.Users = new SelectList(repo.GetUsers(), "UserID", "UserName");
playerView.SelectedUserID = player.User.UserID.ToString();
return View(playerView);

Upvotes: 3

Related Questions