I have these VM properties
public IList<Guid> SelectedEligiableCategories { get; set; } public IList<SelectListItem> EligiableCategories { get; set; }
I have these helpers in my view
@Html.LabelFor(x => x.EligibleCategoryFrmVm.SelectedEligiableCategories, "Eligible Categories:") @Html.ListBoxFor(x => Model.EligibleCategoryFrmVm.SelectedEligiableCategories, Model.EligibleCategoryFrmVm.EligiableCategories, new { @class = "eligibleCategoryListBox" })
I have this code in the controller
List<SelectListItem> eligibleCategoriesListItems = Mapper.Map<List<EligibleCategory>, List<SelectListItem>>(eligibleCategories); foreach (var rewardTier in creditCard.RewardTiers) { CbRewardTierFrmVm rewardTierFrmVm = new CbRewardTierFrmVm(); rewardTierFrmVm.EligibleCategoryFrmVm.EligiableCategories = eligibleCategoriesListItems; foreach (var ec in rewardTier.EligibleCategories) { rewardTierFrmVm.EligibleCategoryFrmVm.SelectedEligiableCategories.Add(ec.Id); } vm.CbRewardTierFrmVm.Add(rewardTierFrmVm); }
But when I upload my opinion. None of the values ββfor my list have been selected. I do not know why. If it were a selectList, it would work, since it would match the SelectedEligiableCategories value in the list.
I'm not sure if this is due to the fact that there are several options
Edit
<select name="CbRewardTierFrmVm[63b504c0-0f9a-47ba-a8ff-db85f48d5f0f].EligibleCategoryFrmVm.SelectedEligiableCategories" multiple="multiple" id="CbRewardTierFrmVm_63b504c0-0f9a-47ba-a8ff-db85f48d5f0f__EligibleCategoryFrmVm_SelectedEligiableCategories" data-val-required="Must choose at least one eligible category." data-val="true" class="eligibleCategoryListBox ui-wizard-content ui-helper-reset ui-state-default" style="display: none;"> <option value="ed2bb5f9-4565-4f69-ab15-9fca011c0692">Gas</option> </select>
-47ba-a8ff-db85f48d5f0f] .EligibleCategoryFrmVm.SelectedEligiableCategories" multiple = "multiple" id = "CbRewardTierFrmVm_63b504c0-0f9a-47ba-a8ff-db85f48d5f0f__EligibleCategoryFrmVm_SelectedEligiableCategories" data-val-required = "Must choose at <select name="CbRewardTierFrmVm[63b504c0-0f9a-47ba-a8ff-db85f48d5f0f].EligibleCategoryFrmVm.SelectedEligiableCategories" multiple="multiple" id="CbRewardTierFrmVm_63b504c0-0f9a-47ba-a8ff-db85f48d5f0f__EligibleCategoryFrmVm_SelectedEligiableCategories" data-val-required="Must choose at least one eligible category." data-val="true" class="eligibleCategoryListBox ui-wizard-content ui-helper-reset ui-state-default" style="display: none;"> <option value="ed2bb5f9-4565-4f69-ab15-9fca011c0692">Gas</option> </select>
What do you think, because I'm using http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/ ?
Edit2
I went ahead and give an example. I have to miss something (I donβt know what). When I use Darin Dimitrov, it works.
I switched this example to a drop-down list since I have a problem with it too.
In this example, I do not use the viewmodel, since my initial assumption was somehow an assistant that I used from Stephen Sanders, maybe this affected me, so I left his example.
This does not seem to be the case since I deleted it and still get this problem.
public class Gift { public string Name { get; set; } public double Price { get; set; } public string SelectedItem { get; set; } public IList<SelectListItem> Items { get; set; } } public ActionResult Index() { List<SelectListItem> items = new List<SelectListItem> { new SelectListItem {Value = "",Text ="--"}, new SelectListItem {Value = "1",Text ="1"}, new SelectListItem {Value = "2",Text ="2"}, }; var initialData = new[] { new Gift { Name = "Tall Hat", Price = 39.95, Items = items, SelectedItem = "2" }, new Gift { Name = "Long Cloak", Price = 120.00, Items = items, SelectedItem = "1" } }; return View("Index3",initialData); } @model IList<EditorDemo.Models.Gift> @{ ViewBag.Title = "Index3"; } @for (int i = 0; i < Model.Count; i++) { @Html.DropDownListFor(x => x[i].SelectedItem, new SelectList(Model[i].Items, "Value", "Text")) }
It seems that you will not be able to process when you put it in forloop, and try to make more than one drop-down list.