I have spent a lot of time scouring the various forums for help on MultiSelectLists in asp.net MVC3. Finally figured out how to solve my issue now I'm trying to cut down on my code and I've come across something weird with ViewBags. First the code, than I'll explain the behavior.

My function that creates the MultiSelectList to used by the Controller

Keep in mind I've cut out a large chunk of code from the edit Action, I have roughly 9 list boxes I'm creating for this security role manager.

My goal, in the edit action is to simply have 1 line of code, calling the GetPermissionList and having it return to the viewbag so I can just display that in the view, as opposed to the 2 lines per listbox that I currently have.

Just looking at the code, it seems obvious if I were to make the call this way:

if you set a breakpoint right after the linke ViewBag.OrdersListBox = GetPermissionList(securityroles.Orders.Split(',')); does it have a value? What about just adding a ToArray() to the end of the split. just a shot in the dark
–
Ryand.JohnsonDec 28 '11 at 19:04

I tried adding ToArray(), same issue with no values being selected. I also added a breakpoint at the suggested line and the values being passed are correct. I just can't wrap my head around the logic why cutting out that first ViewBag object won't work.
–
hyro78Dec 28 '11 at 19:34

1 Answer
1

I think, You have to set Orders because that is what the selected values are being bound to when the selection happens. You could just pass a string[] if you didn't want to have anything preselected. Check out Darin's answer in this post. He is using a model but i think the same concept applies to view bag.