我很难弄清楚如何做到这一点。那里的例子并没有帮助我理解我需要什么。
查看模型
public class ViewModelAddUser
{
StudentSchedulingDBContext ssDB = new StudentSchedulingDBContext();
public User User { get; private set; }
public SelectList Departments { get; private set; }
public ViewModelAddUser()
{
// Not Sure what to put here
}
}
表的 DBContext(只是为了在代码中显示表结构)
public class Department
{
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
}
控制器
public ActionResult AddUser()
{
return View(new ViewModelAddUser());
}
我不太确定如何形成其余部分。我一直在尝试 nerddinner 和其他问题,但我没有让这个工作。
更新
**View Model:**
public class ViewModelAddUser
{
public IEnumerable<SelectListItem> Departments { get; set; }
}
控制器:
public ActionResult AddUser()
{
ViewModelAddUser model = new ViewModelAddUser()
{
Departments = db.Departments.Select(department => new SelectListItem {
Value = department.DepartmentID.ToString(),
Text = department.DepartmentName
})
};
return View(model);
}