好的,我有一种情况,我需要有多个使用同一字典的“BayTypes”的 DDL,这不是问题。每个 'n' BayOptions 一个 DDL。我将字典作为“BayTypes”传递给我的视图,如下所示:
(控制器)
var bayTypes = _bayTypeRepository.GetBayTypes().ToList();
property.BayTypes = bayTypes.ToDictionary(g => g.Name, g => g.BayTypeGuid.ToString());
(看法)
var overrideValue = item.BayTypeOverride ? item.BayTypeOverrideValue.BayTypeGuid.ToString() : string.Empty;
var result = (from x in Model.BayTypes
select new SelectListItem()
{
Text = x.Key,
Value = x.Value,
Selected = x.Value == overrideValue <-- ***this is working***
});
if (item.BayTypeOverride == true)
{
@Html.DropDownListFor(x => x.BayTypes, result, new { @Name = "BayOptionsToSubmit[" + aCounter + "].BayTypeOverrideValue" })
}
else
{
@Html.DropDownListFor(x => x.BayTypes, result, new { @Name = "BayOptionsToSubmit[" + aCounter + "].BayTypeOverrideValue", @style = "display:none;" })
}
在“结果”对象中选择了正确的项目。如果我逐步进行并观看“结果”,我可以看到正确的“Selected = true”......但它在渲染时没有在 DDLFor 中选择......
我错过了什么?