I have a typical scenario where I have a parent entity that can map to multiple child entities. On the parent edit form I have a ListBoxFor that shows all the possible child entities. The user can select more than one option and post the form. At the moment
i've got it so the return types are an int (the parent id) and formcollection. I then use UpdateModel to perform the update and then save. All is good apart from the fact the ListBox is ignored completely. I can see in the formcollection that the listbox is
listed in the keys but how do I get it, presumable cast it and iterate through the selected items create child entities for each?
Model is:
public class RouteFormViewModel
{
//Properties
public Route Route { get; private set; }
public IEnumerable<SelectListItem> RouteTypes { get; set; }
//Constructor
public RouteFormViewModel(Route route)
{
GenericRepository genericRepository = new GenericRepository();
Route = route;
RouteTypes = genericRepository.FindAllRouteTypes();
}
}
Indeed you're right about RouteTypes being a simple dropdown and therefore IEnumerable works fine. To answer your previous question RouteTypeMapping (poorly named) is meant to be a collection of RouteTypes attached to the main Route entity. It's a one to
many relationship. The RouteTypeMapping is a simple table of IDs - mappingID, routeID, routeTypeID.
The goal is to post back the updated Route plus it's RouteTypeMapping child items.
drazic19
Member
529 Points
128 Posts
Pulling ListBox selected items from FormCollection
Aug 01, 2011 11:58 AM|LINK
Hi,
I have a typical scenario where I have a parent entity that can map to multiple child entities. On the parent edit form I have a ListBoxFor that shows all the possible child entities. The user can select more than one option and post the form. At the moment i've got it so the return types are an int (the parent id) and formcollection. I then use UpdateModel to perform the update and then save. All is good apart from the fact the ListBox is ignored completely. I can see in the formcollection that the listbox is listed in the keys but how do I get it, presumable cast it and iterate through the selected items create child entities for each?
Model is:
public class RouteFormViewModel { //Properties public Route Route { get; private set; } public IEnumerable<SelectListItem> RouteTypes { get; set; } //Constructor public RouteFormViewModel(Route route) { GenericRepository genericRepository = new GenericRepository(); Route = route; RouteTypes = genericRepository.FindAllRouteTypes(); } }The View is:
<p> <%: Html.Label("", "Categories") %> <%: Html.ListBoxFor(model => model.Route.RouteTypeMappings, Model.RouteTypes) %> </p>The POST Controller is:
public ActionResult Edit(int id, FormCollection collection) { //Get Route Route route = routeRepository.GetRoute(id); // check model state if (ModelState.IsValid) { try { //update route UpdateModel(route, "Route"); // ** Need to save all selected items from listbox ** routeRepository.Save(); return RedirectToAction("View", new { id = route.rID }); } catch (Exception e){ return View(new RouteFormViewModel(route)); } } return View(new RouteFormViewModel(route)); }Any help of suggestions would be great.
Thanks,
Michael
mvc
bruce (sqlwo...
All-Star
36656 Points
5438 Posts
Re: Pulling ListBox selected items from FormCollection
Aug 01, 2011 04:06 PM|LINK
what is the definition of RouteTypeMappings? it should be a List<> or array.
mvc
ignatandrei
All-Star
134527 Points
21579 Posts
Moderator
MVP
Re: Pulling ListBox selected items from FormCollection
Aug 01, 2011 08:30 PM|LINK
MVC ( and you...) does not know what particular class implements IEnumerable from the multiple options(array, List, Set, and so on)
So please change to
public List<SelectListItem> RouteTypes { get; set; }
bruce (sqlwo...
All-Star
36656 Points
5438 Posts
Re: Pulling ListBox selected items from FormCollection
Aug 01, 2011 09:21 PM|LINK
in the above code RouteTypes is the dropdown's list values, so IEnumerable is fine.
drazic19
Member
529 Points
128 Posts
Re: Pulling ListBox selected items from FormCollection
Aug 04, 2011 03:02 PM|LINK
Hi Bruce,
Indeed you're right about RouteTypes being a simple dropdown and therefore IEnumerable works fine. To answer your previous question RouteTypeMapping (poorly named) is meant to be a collection of RouteTypes attached to the main Route entity. It's a one to many relationship. The RouteTypeMapping is a simple table of IDs - mappingID, routeID, routeTypeID.
The goal is to post back the updated Route plus it's RouteTypeMapping child items.
Thanks for the help.