选择列表的字典
Dictionary to 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();
}
}
如何转换为 select 列表。我尝试了下面的代码,但是当我检查视图时,它在 selectlist
中显示为 ["text","value]
的值
例如:- [Tom,3]
ViewBag.activity_type = new SelectList(activitylist);
已编辑
我也试过了
ViewBag.activity_type = new SelectList(activitylist,"Key","Value");
然后在下拉列表中显示 3..我需要显示 Tom
你很接近,试试这个。
ViewBag.activity_type = new SelectList(activitylist, "Key", "Value");
如果要在下拉列表中显示 Tom,请交换键和值。
ViewBag.activity_type = new SelectList(activitylist, "Value", "Key");
在 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();
}
}
如何转换为 select 列表。我尝试了下面的代码,但是当我检查视图时,它在 selectlist
中显示为 ["text","value]
的值
例如:- [Tom,3]
ViewBag.activity_type = new SelectList(activitylist);
已编辑
我也试过了
ViewBag.activity_type = new SelectList(activitylist,"Key","Value");
然后在下拉列表中显示 3..我需要显示 Tom
你很接近,试试这个。
ViewBag.activity_type = new SelectList(activitylist, "Key", "Value");
如果要在下拉列表中显示 Tom,请交换键和值。
ViewBag.activity_type = new SelectList(activitylist, "Value", "Key");