Skip to content Skip to sidebar Skip to footer

My Dropdownlistfor Does Not Select The Selectedvalue From My Selectlist

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

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

Post a Comment for "My Dropdownlistfor Does Not Select The Selectedvalue From My Selectlist"