我的DropDownListFor没有从我的SelectList中选择SelectedValue,它总是选择第一个项目。任何解决方案?
答案 0 :(得分:1)
例如,这不起作用(默认为第一项)
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);