2013-06-06 31 views
0

My DropDownListFor不會從我的SelectList中選擇SelectedValue,而是始終選擇第一個項目。任何解決方案My DropDownListFor不會從我的SelectList中選擇SelectedValue

enter image description here

+0

顯示代碼圖片,你可以複製和粘貼它作爲文本讓我難過。這個問題很少有機會幫助其他開發者,除非谷歌在你的圖像上執行OCR)。在手機上閱讀也很困難... –

+0

抱歉讓你難過。 –

回答

1

嗯,實際上,經過再三考慮,仔細檢查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); 
+0

你的意思是SelectList中的SelectedValue被忽略了嗎? –

+0

我試圖將對象本身綁定爲選定的項目,但不適合我。我的猜測是,下拉列表綁定到一個int類型,但所選列表是內容對象類型,這就是爲什麼視圖無法將Id綁定爲選定值<%:Html.DropDownListFor(model => model.SubLedgerType_Id ,新的SelectList(Model.GetActiveSubLedgerType(),「Id」,「Code」,Model.SubLedgerType_Id))%> –

+0

我的錯誤,我綁定了錯誤的ID。您的解決方案完美運作 –

相關問題