简体   繁体   English

我的DropDownListFor没有从我的SelectList中选择SelectedValue

[英]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. 我的DropDownListFor不会从我的SelectList中选择SelectedValue,它总是选择第一项。 Any solution? 有什么办法吗?

在此处输入图片说明

hmm, actually, on second thought, double check sublegerType_Id? 嗯,实际上,再三考虑,仔细检查sublegerType_Id? I'm not sure it should be the Id. 我不确定它应该是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)) 但这可以正常工作(默认为(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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM