My DropDownListFor不会从我的SelectList中选择SelectedValue,而是始终选择第一个项目。任何解决方案My DropDownListFor不会从我的SelectList中选择SelectedValue
回答
嗯,实际上,经过再三考虑,仔细检查sublegerType_Id?我不确定它应该是Id。我认为它需要成为实际的对象。
例如,这不工作(默认为第一项)
List<Tuple<string, string>> NumberList = new List<Tuple<string, string>>();
for (int i = 0; i < 5; i++)
{
NumberList.Add(new Tuple<string, string>(i.ToString(),i.ToString()));
}
NumberSelectList = new SelectList(NumberList,"2");
但这工作好(默认为的(4,4)所选择的项目)
List<Tuple<string, string>> NumberList = new List<Tuple<string, string>>();
Tuple<string, string> selectedObject = new Tuple<string, string>("-1","-1");
for (int i = 0; i < 5; i++)
{
if (i == 4)
{
selectedObject = new Tuple<string, string>(i.ToString(), i.ToString());
NumberList.Add(selectedObject);
}
else
NumberList.Add(new Tuple<string, string>(i.ToString(), i.ToString()));
}
NumberSelectList = new SelectList(NumberList, selectedObject);
你的意思是SelectList中的SelectedValue被忽略了吗? –
我试图将对象本身绑定为选定的项目,但不适合我。我的猜测是,下拉列表绑定到一个int类型,但所选列表是内容对象类型,这就是为什么视图无法将Id绑定为选定值<%:Html.DropDownListFor(model => model.SubLedgerType_Id ,新的SelectList(Model.GetActiveSubLedgerType(),“Id”,“Code”,Model.SubLedgerType_Id))%> –
我的错误,我绑定了错误的ID。您的解决方案完美运作 –
- 1. DropDownListFor未选择SelectList中的选定项目
- 2. 的SelectList ingoring的SelectedValue
- 3. 即使对象已选择= true,我的MVC SelectList不会选择?
- 4. MVC3 DropDownlistFor SelectedValue
- 5. DropdownListFor不会选择(谁会猜对?)
- 6. MVC3 DropDownListFor不填充selectedValue
- 7. Dropdownlistfor不会显示正确的选择
- 8. DropDownListFor选择的值不会名单
- 9. 的SelectList选择的值不从整数
- 10. DropDownListFor并显示SelectedValue
- 11. Kendo UI DropDownListFor Set SelectedValue
- 12. MVC3剃刀的SelectedValue在DropdownListFor
- 13. 如何为SelectList指定SelectedValue?
- 14. DropDownListFor不选择值时,从收集
- 15. MVC DropDownListFor SelectedValue收集问题
- 16. 的DropDownList的SelectList的SelectedValue问题
- 17. DropDownListFor - 不选择“选定”的值
- 18. MVC DropDownListFor未从模型中选择值
- 19. 的SelectList对象了selectedValue问题
- 20. 的SelectList与空选择
- 21. DropDownListFor使用会话状态的SelectedValue和Disable
- 22. ASP.NET MVC DropDownListFor不选择Prepoulated价值
- 23. DropDownListFor未选择所选
- 24. MVC3 DropDownListFor的SelectedItem非选择
- 25. SelectList不会接受我的SelectListItem对象
- 26. DropDownList的SelectedValue不是我在Enabled = false时选择的值
- 27. 为什么AspNetCore SelectList的selectedValue参数不起作用?
- 28. Razor DropDownListFor:将额外属性添加到SelectList选项标签
- 29. MVC DropDownListFor不选择一个值
- 30. DropDownListFor选择不起作用 - 再次
显示代码图片,你可以复制和粘贴它作为文本让我难过。这个问题很少有机会帮助其他开发者,除非谷歌在你的图像上执行OCR)。在手机上阅读也很困难... –
抱歉让你难过。 –