我想将数据库中的数据绑定到 DropDownList ,我尝试了以下方法,但数据未绑定到下拉列表,谁能告诉我我在哪里做错了
这是我的控制器
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult AddNew()
{
ViewBag.RoleName = new SelectList(GetRoles(), "RoleID", "RoleName");
return View();
}
public List<SelectListItem> GetRoles()
{
var roles = new List<SelectListItem>();
SqlConnection conn = new SqlConnection("Data Source=LMIT-0039;Initial Catalog=BugTracker;Integrated Security=True");
SqlCommand Cmd = new SqlCommand("Select GroupId,EmplopyeeRole from EmployeeGroup", conn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(Cmd);
DataSet ds = new DataSet();
da.Fill(ds);
for (int i = 0; i <= ds.Tables[0].Rows.Count-1; i++)
{
var model = new ResourceModel();
model.RoleId = Convert.ToInt16(ds.Tables[0].Rows[i]["GroupId"]);
model.RoleName = ds.Tables[0].Rows[i]["EmplopyeeRole"].ToString();
}
conn.Close();
return roles;
}
这是我的模型
public class ResourceModel
{
public static List<ResourceModel> GetList { get; set; }
public Int16 EmployeeId { get; set; }
public string EmployeeName { get; set; }
public string EmployeeEmailId { get; set;}
public string GroupName { get; set; }
public string EmployeePassword { get; set; }
public static List<SelectListItem> GetRoles { get; set; }
public int RoleId { get; set; }
public string RoleName { get; set; }
}
这是我的 aspxPage
<%:Html.DropDownList("RoleName")%>
当我放一个断点并看到我在 GetRoles 方法中有数据时..谁能告诉我我在哪里做错了