My DropDownListFor does not select the SelectedValue from my SelectList

梦想与她 提交于 2019-12-12 01:04:20

问题


My DropDownListFor does not select the SelectedValue from my SelectList, it always selected the first item instead. Any solution?


回答1:


hmm, actually, on second thought, double check sublegerType_Id? I'm not sure it should be the Id. I think it needs to be the actual object.

for example, this doesnt work (it defaults to the first item)

        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");

but this works okay (it defaults to the selected item of (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);


来源:https://stackoverflow.com/questions/16952373/my-dropdownlistfor-does-not-select-the-selectedvalue-from-my-selectlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!