-->

My DropDownListFor does not select the SelectedVal

2019-08-31 02:39发布

问题:

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