ASP.NET MVC 3
I have three tables with following class:
Employee.cs
Department.cs
Designation.cs
Employee.cs have below properties
public int EmployeeID { get; set; }
public string EmpNo { get; set; }
public string EmployeeACN { get; set; }
public string EmployeeName { get; set; }
public Nullable<int> DeptID { get; set; }
public Nullable<int> DesID { get; set; }
public string ReportingTo { get; set; }
public string MailId { get; set; }
public virtual Department Department { get; set; }
public virtual Designation Designation { get; set; }
In EmployeeController.cs
public ActionResult Edit(int id)
{
Employee employee = db.Employees.Find(id);
ViewBag.DeptID = new SelectList(db.Departments, "DeptID", "DeptShortDesc", employee.DeptID);
ViewBag.DesID = new SelectList(db.Designations, "DesID", "ShortDesc", employee.DesID);
return View(employee);
}
[HttpPost]
public ActionResult Edit(Employee employee)
{
if (ModelState.IsValid)
{
db.Entry(employee).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.DeptID = new SelectList(db.Departments, "DeptID", "DeptShortDesc", employee.DeptID);
ViewBag.DesID = new SelectList(db.Designations, "DesID", "ShortDesc", employee.DesID);
return View(employee);
}
My Question is :
How to add "EmployeeName" list as DropDowlList for "ReportingTo" property of class Employee.
Elabolate:
In edit View I get drop down list for "department name" and "Designation" with respect to DeptID and DesID.
Similarly I want "ReportingTo" field, gets the list of "EmployeeNames" in DropDownlist from the same Employee class.
***
I can get the dropdown list of Employee Names for Reporting To column but on selection it does not gets saved to database.
I did below changes in Employeecontroler:
Added below line of code in Edit Action result.
ViewBag.EmpNo = new SelectList(db.Employees, "EmpNo", "EmployeeName", employee.EmpNo);
and in Edit View
<div class="editor-label">
@Html.LabelFor(model => model.EmpNo,"ReportingTo")
</div>
<div class="editor-field">
@Html.DropDownList("EmpNo", String.Empty)
@Html.ValidationMessageFor(model => model.EmpNo)
</div>