在Mvc C中从DataTable填充DropDownList#

时间:2015-07-23 06:11:59

标签: c# asp.net asp.net-mvc

connectionHelper.Select(query)返回datatable。我想用empList填充我的下拉列表。我该怎么办?

            var query = "SELECT ADSOYAD, EMPLOYEE_NUMBER FROM TTF_PERSONEL";
            var employeeNames = connectionHelper.Select(query);

            var empList = new List<CalisanModels>();
            var emp = new CalisanModels();

            for (int i = 0; i < employeeNames.Rows.Count; i++)
            {
                var data = employeeNames.Rows[i];

                emp.EmployeeNumber = Convert.ToInt32(data["EMPLOYEE_NUMBER"]);
                emp.AdSoyad = data["ADSOYAD"].ToString();

                empList.Add(emp);
            }

1 个答案:

答案 0 :(得分:2)

所以你现在已经填满了empList,你可以将它发送给View以供呈现。

从你的行动中设定:

ViewBag.EmployeeList = empList;

在您的视图中渲染:

@Html.DropDownList("Employees", new SelectList((IEnumerable<MyMvcApp.CalisanModels>)ViewBag.EmployeeList, "EMPLOYEE_NUMBER", "ADSOYAD"))

我在此之前回答的类似下拉人群:https://dotnetfiddle.net/yPwHDB