MVC加载下拉列表

时间:2023-11-11 14:31:14
        public ActionResult GetList()
{
string strSql = "select staff_id,nick_name from tbl_ac_info where status='A'";
SqlDataReader sdr = SqlDbHelper.GetSqlDataReader(strSql);
List<SelectListItem> list = new List<SelectListItem>();
while (sdr.Read())
{
SelectListItem listSub = new SelectListItem();
listSub.Text = sdr["nick_name"].ToString();
listSub.Value = sdr["staff_id"].ToString();
list.Add(listSub);
}
ViewBag.list = new SelectList(list, "value", "text","my");
return View();
}
 用户名:@Html.DropDownList("ddlStaff_id", (SelectList)ViewBag.list, "请选择")

生成效果:

用户名:<select id="ddlStaff_id" name="ddlStaff_id"><option value="">请选择</option>
<option value="jacky">MyJacky</option>
<option selected="selected" value="my">my</option>
<option value="my001">jacky</option>
<option value="my002">jacky</option>
<option value="myjacky">jacky</option>
</select>