简体   繁体   中英

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

Try like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM