Note that there are some explanatory texts on larger screens.

plurals
  1. POChallenges with selecting values in ListBoxFor
    primarykey
    data
    text
    <p>Working on my first ASP.Net MVC2 web app recently, I came across some issues when I needed to select multiple values in a list box. I worked around it with some jQuery, but went ahead and put together some very simple code to demonstrate. I'm using EF for the model, with two entities - Customers and HelpDeskCalls:</p> <p>Controller:</p> <pre><code> public ActionResult Edit(int id) { Customer currCustomer = ctx.Customers.Include("HelpDeskCalls").Where(c =&gt; c.ID == id).FirstOrDefault(); List&lt;HelpDeskCall&gt; currCustCalls = (ctx.HelpDeskCalls.Where(h =&gt; h.CustomerID == id)).ToList(); List&lt;SelectListItem&gt; currSelectItems = new List&lt;SelectListItem&gt;(); List&lt;String&gt; selectedValues = new List&lt;string&gt;(); foreach (HelpDeskCall currCall in currCustCalls) { bool isSelected = (currCall.ID % 2 == 0) ? true : false; //Just select the IDs which are even numbers... currSelectItems.Add(new SelectListItem() { Selected = isSelected, Text = currCall.CallTitle, Value = currCall.ID.ToString() }); //add the selected values into a separate list as well... if (isSelected) { selectedValues.Add(currCall.ID.ToString()); } } ViewData["currCalls"] = (IEnumerable&lt;SelectListItem&gt;) currSelectItems; ViewData["currSelected"] = (IEnumerable&lt;String&gt;) selectedValues; return View("Edit", currCustomer); } </code></pre> <p>View:</p> <pre><code>&lt;div class="editor-field"&gt; &lt;%: Html.ListBoxFor(model =&gt; model.HelpDeskCalls, new MultiSelectList(Model.HelpDeskCalls, "ID", "CallTitle", (IEnumerable) ViewData["currSelected"]), new { size = "12" })%&gt; &lt;%: Html.ListBoxFor(model =&gt; model.HelpDeskCalls, ViewData["currCalls"] as IEnumerable&lt;SelectListItem&gt;, new { size = "12"}) %&gt; &lt;%: Html.ListBox("Model.HelpDeskCalls", new MultiSelectList(Model.HelpDeskCalls, "ID", "CallTitle", (IEnumerable)ViewData["currSelected"]), new { size = "12"}) %&gt; &lt;%: Html.ValidationMessageFor(model =&gt; model.HelpDeskCalls) %&gt; &lt;/div&gt; </code></pre> <p>For this sample, I'm just selecting HelpDeskCall.IDs which are even. I'm trying two different syntaxes for ListBoxFor: One uses an IEnumerable of values for selections, one using an IEnumerable of SelectListItems. By default, when I run this code, no selections are made to either ListBoxFor, but the non-strongly typed ListBox selects correctly.</p> <p>I read <a href="http://forums.asp.net/t/1570736.aspx" rel="nofollow noreferrer">this post</a> on ASP.Net and <a href="https://stackoverflow.com/questions/2308846/why-is-listboxfor-not-selecting-items-but-listbox-is">this thread</a> on SO, but no joy. In fact, if I add the override ToString() to my HelpDeskCall class (as suggested in the ASP.net thread) all values are selected, which isn't right either.</p> <p>If someone could shed some light on how this should work (and what I'm missing or doing wrong), this then neophyte would be very grateful.</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