Note that there are some explanatory texts on larger screens.

plurals
  1. POViewBag oddity in creating a MultiSelectList
    primarykey
    data
    text
    <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 = "", Text = "None"}, new SelectListItem{ Value = "View", Text = "View"}, new SelectListItem{ Value = "Add", Text = "Add"}, new SelectListItem{ Value = "Edit", Text = "Edit"}, new SelectListItem{ Value = "Delete", Text = "Delete"} }; return new MultiSelectList(permissions, "Value", "Text", 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 = "Edit a Security Role"; 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="rightAlign topAlign editor-label"&gt; @Html.MyLabel(m =&gt; m.Orders, "lblOrders") &lt;/td&gt; &lt;td class="editor-field"&gt; @Html.ListBoxFor(m =&gt; m.Orders, ViewBag.OrdersListBox as MultiSelectList, new { size = "5" }) &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>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload