2013-06-06 27 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。您的解决方案完美运作 –

相关问题