字典选择列表

时间:2015-07-01 08:40:32

标签: asp.net-mvc dictionary selectlist

在MVC中,我正在创建一个字典,如下所示

Dictionary<string, long> activitylist = new Dictionary<string, long>();
using (OracleConnection con = AppConn.Connection)
{
    OracleCommand cmd = con.CreateCommand();
    cmd.Connection = con;
    try
    {
        con.Open();
        cmd.CommandText = "Select activity_id,activity  from apps_live.im_activity_types_v where activity_id != 12 order by activity";
        OracleDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            activitylist.Add(reader[1].ToString(), Convert.ToInt32(reader[0]));
        }
    }
    catch (Exception e)
    {
    }
    finally
    {
        con.Close();
    }
}

如何将其转换为选择列表。我尝试了下面的代码,但是当我查看视图时,它在["text","value]中显示的值为selectlist 例如: - [Tom,3]

ViewBag.activity_type = new SelectList(activitylist);

被修改

我也试过

ViewBag.activity_type = new SelectList(activitylist,"Key","Value");

然后在下拉列表中显示3..i需要显示Tom

1 个答案:

答案 0 :(得分:1)

你很亲密,试试这个。

ViewBag.activity_type = new SelectList(activitylist, "Key", "Value");

如果您想在下拉列表中显示Tom,请交换Key和Value。

ViewBag.activity_type = new SelectList(activitylist, "Value", "Key");