我正在尝试将开发不佳的 C# Web 应用程序转换为 MVC2,并且在尝试构建级联下拉列表时遇到了困难。这是代码:
public IEnumerable<SelectListItem> SchoolList
{
get
{
DataTable dt = ClassModels.GetSchools();
List<SelectListItem> list = new List<SelectListItem>();
foreach (DataRow row in dt.Rows)
{
list.Add(new SelectListItem
{
Text = Convert.ToString(row["School"]),
Value = Convert.ToString(row["SID"]),
});
}
return list;
}
}
public IEnumerable<SelectListItem> DepartmentList
{
get
{
DataTable dt = DomData.GetDepartments(this is where the selected SID goes)
List<SelectListItem> list = new List<SelectListItem>();
foreach (DataRow row in dt.Rows)
{
list.Add(new SelectListItem
{
Text = Convert.ToString(row["Department"]),
Value = Convert.ToString(row["DID"]),
});
}
return list;
}
}
这是部门的模型
public static DataTable GetDepartments(int id)
{
string sql = string.Format(@"SELECT d.department, d.did
FROM dept d
WHERE d.did IN (SELECT DISTINCT(DID) FROM CDS WHERE SID = '{0}')
ORDER BY department", id);
DataTable db_table = new DataTable("departments");
SqlDataAdapter db_adapter = new SqlDataAdapter(sql, iau_conxn_string);
db_adapter.Fill(db_table);
//ddl_dep.DataSource = db_table;
//ddl_dep.DataValueField = "did";
//ddl_dep.DataTextField = "department";
//ddl_dep.DataBind();
return db_table;
}
可悲的是,我必须在没有 AJAX 或 jQuery 的情况下这样做。