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)。在手機上閱讀也很困難... –
抱歉讓你難過。 –