Asp – Selected Item doesnt get selected

asp.net-mvc

I have a problem with a selectlist, I have 8 items and 3 of them gets the value selected = true in debug, but the rendered Html the item isnt selected.

What might be wrong?

List<SelectListItem> UsergroupID = (from usg in _ug.GetUsergroups().ToList()
                                            join ug in u.Usergroups
                                            on usg.UsergroupID equals ug.UsergroupID into j
                                            select
                                            new SelectListItem
                                            {
                                                Selected = j.Any(),
                                                Value = usg.UsergroupID.ToString(),
                                                Text = usg.UsergroupName
                                            }).ToList();

        ViewData["UsergroupID"] = UsergroupID;

        return View("UserEdit", new UserAdminEditViewModel { User = u, Usergroups = _ug.GetUsergroups() });

And in my view I have:

<%= Html.ListBox("UsergroupID", (IEnumerable<SelectListItem>)ViewData["UsergroupID"]) %>

What's the reason for it not making the 3 items that have selected = true selected in the selectlist?
/M

Best Answer

looks like another bug in MVC to me ... you can refer to this link explaining part of the issue Link

Related Topic