我有两个模型定义如下:
public class Division
{
public int DivisionID { get; set; }
[Required]
public string DivisionName { get; set; }
public virtual Employee Contact{ set;get; }
public virtual ICollection<Employee> Employees { get; set; }
}
public class Employee
{
public int EmployeeID{ get; set; }
public string Name{ get; set; }
public virtual Division Division{set;get;}
}
实体框架在部门表中设置了一个名为 的字段employee_employeeid
,如何为部门表中的联系人属性创建员工下拉列表。
这是我尝试过的,但没有任何东西被发送到数据库。
ViewBag.contact = new SelectList(db.Employees,"EmployeeID","Name");
在我看来:
@Html.DropDownList("contact",String.Empty)
我必须使用命名约定吗?
编辑
发布操作:
[HttpPost]
public ActionResult Edit(Division division)
{
if (ModelState.IsValid)
{
db.Entry(division).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.employeeid = new SelectList(
db.Employees, "EmployeeID", "EmployeeFirstName", division.employee);
return View(division);
}