Sachu
Sachu

Reputation: 7766

Dictionary to selectlist

In MVC I am creating a dictionary as below

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

How can convert the same as a select list. I tried below code but when i check the view it is showing value as ["text","value] in the selectlist eg:- [Tom,3]

ViewBag.activity_type = new SelectList(activitylist);

Edited

I also tried

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

then in the dropdown it is showing 3..i need to show Tom

Upvotes: 0

Views: 1710

Answers (1)

Nitin Varpe
Nitin Varpe

Reputation: 10694

You were close, try this.

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

If you want to show Tom in dropdown, interchange Key and Value.

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

Upvotes: 2

Related Questions