Problems with choosing values ​​in ListBoxFor

This is my first question, and it is probably bad, so please be careful.

While working on my first ASP.Net MVC2 web application recently, I ran into some problems when I needed to select multiple values ​​from a list. I worked on it using some jQuery, but went ahead and put together a very simple code for demonstration. I use EF for a model with two objects - Clients and HelpDeskCalls:

Controller:

public ActionResult Edit(int id) { Customer currCustomer = ctx.Customers.Include("HelpDeskCalls").Where(c => c.ID == id).FirstOrDefault(); List<HelpDeskCall> currCustCalls = (ctx.HelpDeskCalls.Where(h => h.CustomerID == id)).ToList(); List<SelectListItem> currSelectItems = new List<SelectListItem>(); List<String> selectedValues = new List<string>(); 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<SelectListItem>) currSelectItems; ViewData["currSelected"] = (IEnumerable<String>) selectedValues; return View("Edit", currCustomer); } 

View:

 <div class="editor-field"> <%: Html.ListBoxFor(model => model.HelpDeskCalls, new MultiSelectList(Model.HelpDeskCalls, "ID", "CallTitle", (IEnumerable) ViewData["currSelected"]), new { size = "12" })%> <%: Html.ListBoxFor(model => model.HelpDeskCalls, ViewData["currCalls"] as IEnumerable<SelectListItem>, new { size = "12"}) %> <%: Html.ListBox("Model.HelpDeskCalls", new MultiSelectList(Model.HelpDeskCalls, "ID", "CallTitle", (IEnumerable)ViewData["currSelected"]), new { size = "12"}) %> <%: Html.ValidationMessageFor(model => model.HelpDeskCalls) %> </div> 

For this example, I simply select HelpDeskCall.ID, which are even. I am trying to use two different syntax for ListBoxFor: One uses IEnumerable values ​​to select, using IEnumerable from SelectListItems. By default, when I run this code, the ListBoxFor are not selected, but the non-strongly typed ListBox selects correctly.

I read this post on ASP.Net and this thread on SO, but no joy. In fact, if I add an override of ToString () to my HelpDeskCall class (as suggested in the ASP.net stream), all values ​​will be selected, which is also not true.

If someone can shed light on how this should work (and what I am missing or doing wrong), this neophyte would be very grateful.

John

+44
asp.net-mvc asp.net-mvc-2
Jul 07 '10 at 11:15
source share
3 answers

Here is an example illustrating a strongly typed version:

Model:

 public class MyViewModel { public int[] SelectedItemIds { get; set; } public MultiSelectList Items { get; set; } } 

Controller:

 public class HomeController : Controller { public ActionResult Index() { // Preselect items with id 1 and 3 var selectedItemIds = new[] { 1, 3 }; var model = new MyViewModel { Items = new MultiSelectList( new[] { // TODO: Fetch from your repository new { Id = 1, Name = "item 1" }, new { Id = 2, Name = "item 2" }, new { Id = 3, Name = "item 3" }, }, "Id", "Name", selectedItemIds ) }; return View(model); } } 

View:

 <%: Html.ListBoxFor(x => x.SelectedItemIds, Model.Items) %> 
+88
Jul 07 2018-10-10T00:
source share

I don't know if this behavior has changed in RTM MVC3, which I use, but it seems that selection and binding now work out of the box. The only catch is that the model should contain a property with identifiers, for example:

 public class MyViewModel { public int[] ItemIDs { get; set; } } 

Then the following in the view will work fine, how to pre-select the correct values ​​and bindings at the time of publication:

 @Html.ListBoxFor(model => model.ItemIDs, (IEnumerable<SelectListItem>)(new[] { new SelectListItem() { Value = "1", Text = "1" }, new SelectListItem() { Value = "2", Text = "2" } })) 
+8
Jan 19 2018-11-11T00:
source share

I found the best workaround. The usual way to preselect a picklist is

 @Html.ListBoxFor( model => model.Roles, new MultiSelectList(db.Roles, "Id", "Name") ) @Html.ValidationMessageFor(model => model.Roles) 

Doesn't work .., no option is ever selected until:

 public ActionResult Edit(int id) { var user = db.Users.Find(id); // this is workaround for http://aspnet.codeplex.com/workitem/4932?ProjectName=aspnet ViewData["Roles"] = user.Roles.Select(r => r.Id); return View(user); } 

Selected roles must be stored in ViewData to get around the nasty bug.

+2
Sep 08 '10 at 15:49
source share



All Articles