title: ViewBag oddity in creating a MultiSelectList
body: <p>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.</p>
  <p><strong>My function that creates the MultiSelectList to used by the Controller</strong></p>
  <pre><code>public MultiSelectList GetPermissionList(string[] selectedValues)
          {
  
              List&lt;SelectListItem&gt; permissions = new List&lt;SelectListItem&gt;()
              {
                  
                  new SelectListItem{ Value = &quot;&quot;, Text = &quot;None&quot;},
                  new SelectListItem{ Value = &quot;View&quot;, Text = &quot;View&quot;},
                  new SelectListItem{ Value = &quot;Add&quot;, Text = &quot;Add&quot;},
                  new SelectListItem{ Value = &quot;Edit&quot;, Text = &quot;Edit&quot;},
                  new SelectListItem{ Value = &quot;Delete&quot;, Text = &quot;Delete&quot;}
              };
              return new MultiSelectList(permissions, &quot;Value&quot;, &quot;Text&quot;, selectedValues);
          }
  </code></pre>
  <p><strong>partial code from the edit action from the controller</strong></p>
  <pre><code>public ActionResult Edit(int id)
          { 
              ViewBag.Title = &quot;Edit a Security Role&quot;;
              SecurityRoles securityroles = Repository.Details(id);
                    
               
              ViewBag.Orders = securityroles.Orders.Split(',');
              ViewBag.OrdersListBox = GetPermissionList(ViewBag.Orders);
               
              return View(securityroles); 
          }
  </code></pre>
  <p><strong>partial code from the View</strong></p>
  <pre><code>&lt;td class=&quot;rightAlign topAlign editor-label&quot;&gt;
    @Html.MyLabel(m =&gt; m.Orders, &quot;lblOrders&quot;)   
  &lt;/td&gt;
  &lt;td class=&quot;editor-field&quot;&gt;  
    @Html.ListBoxFor(m =&gt; m.Orders, ViewBag.OrdersListBox as MultiSelectList, new { size = &quot;5&quot; }) 
  &lt;/td&gt;
          
  
                      
  </code></pre>
  <p>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.</p>
  <p>My goal, in the edit action is to simply have 1 line of code, calling the <strong>GetPermissionList</strong> 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.</p>
  <p>Just looking at the code, it seems obvious if I were to make the call this way:</p>
  <blockquote>
  <p>ViewBag.OrdersListBox = GetPermissionList(securityroles.Orders.Split(','));</p>
  </blockquote>
  <p>It should work, but the selected values do not come through. To compound the oddity, here is something else I tried and it worked fine, but it makes no sense why.</p>
  <blockquote>
  <p>ViewBag.Orders = securityroles.Orders.Split(',');</p>
  <p>ViewBag.OrdersListBox = GetPermissionList(securityroles.Orders.Split(','));</p>
  </blockquote>
  <p><strong>ViewBag.Orders</strong> plays no role in the <strong>ViewBag.OrdersListBox</strong> nor is it used in the view, but when I simply assign it a value than the 2nd line of code works.</p>
  <p>Does this make sense to anyone? Any suggestions on how to create a way for the <strong>GetPermissionList</strong> to simply work correctly by sending it a string array instead of passing it the ViewBag object?</p>