我的DropDownListFor没有从我的SelectList中选择SelectedValue [英] My DropDownListFor does not select the SelectedValue from my SelectList

查看:136
本文介绍了我的DropDownListFor没有从我的SelectList中选择SelectedValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的DropDownListFor不会从我的SelectList中选择SelectedValue,它总是选择第一项.有解决办法吗?

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

推荐答案

嗯,实际上,再三考虑一下,仔细检查sublegerType_Id?我不确定它应该是Id.我认为它必须是实际的对象.

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

但这可以正常工作(默认为(4,4)的选定项)

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

这篇关于我的DropDownListFor没有从我的SelectList中选择SelectedValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆