我有这个VM属性
public IList<Guid> SelectedEligiableCategories { get; set; } public IList<SelectListItem> EligiableCategories { get; set; }
在我看来,我有这个助手
@Html.LabelFor(x => x.EligibleCategoryFrmVm.SelectedEligiableCategories,"Eligible Categories:") @Html.ListBoxFor(x => Model.EligibleCategoryFrmVm.SelectedEligiableCategories,Model.EligibleCategoryFrmVm.EligiableCategories,new { @class = "eligibleCategoryListBox" })
我的控制器中有这个代码
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); }
然而,当我加载我的观点时.没有选择我的ListBox的值.我不知道为什么.如果这是一个selectList,这将起作用,因为它会将SelectedEligiableCategories与列表中的值匹配.
我不确定这是否因为有多个选择
编辑
<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>
你认为这是因为我使用的是http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/吗?
EDIT2
我继续前进并做出榜样.我必须遗漏一些东西(不确定是什么).当我使用“Darin Dimitrov”时它起作用.
我将示例切换为下拉列表,因为我也遇到了同样的问题.
在这个例子中,我没有使用viewmodel,因为我最初的假设是以某种方式从Steven Sanders使用的帮助器可能会影响它所以我离他的例子.
这似乎不是这种情况,因为我删除它仍然得到这个问题.
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,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")) }
当你把它放入forloop并尝试制作多个下拉列表时似乎无法处理.
解决方法
以下适用于我.
模型:
public class Myviewmodel { public IList<Guid> SelectedEligiableCategories { get; set; } public IList<SelectListItem> EligiableCategories { get; set; } }
控制器:
public class HomeController : Controller { public ActionResult Index() { var model = new Myviewmodel { SelectedEligiableCategories = new[] { // preselect the second and the fourth item new Guid("35830042-3556-11E1-BCDC-A6184924019B"),new Guid("4253876A-3556-11E1-BC17-B7184924019B") }.ToList(),EligiableCategories = new[] { new SelectListItem { Value = "2DA62E3A-3556-11E1-8A0A-9B184924019B",Text = "item 1" },new SelectListItem { Value = "35830042-3556-11E1-BCDC-A6184924019B",Text = "item 2" },new SelectListItem { Value = "3D07EBAC-3556-11E1-8943-B6184924019B",Text = "item 3" },new SelectListItem { Value = "4253876A-3556-11E1-BC17-B7184924019B",Text = "item 4" },} }; return View(model); } }
视图:
@model Myviewmodel @using (Html.BeginForm()) { @Html.ListBoxFor( x => x.SelectedEligiableCategories,Model.EligiableCategories,new { @class = "eligibleCategoryListBox" } ) }
结果:
更新:
现在您已经显示了一个允许说明问题的示例,您可以在构建SelectList时指定所选项:
@Html.DropDownListFor( x => x[i].SelectedItem,"Text",Model[i].SelectedItem) )
未预先选择值的原因是因为您将下拉列表绑定到属性列表(x => x [i] .SelectedItem),而在我的示例中,我使用的是简单属性.
如果您想使用ListBoxFor帮助程序执行此操作,则可以使用以下命令:
@Html.ListBoxFor( x => x[i].SelectedItems,new MultiSelectList(Model[i].Items,Model[i].SelectedItems) )
SelectedItems属性成为集合,我们使用MultiSelectList而不是SelectList.