Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ASP.NET MVC 3
I have three tables with following class:
Employee.cs
Department.cs
Designation.cs

Employee.cs have below properties

C#
   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

C#
// GET: /Employee/Edit/5
 
        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);
        }

        //
        // POST: /Employee/Edit/5

        [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.
C#
ViewBag.EmpNo = new SelectList(db.Employees, "EmpNo", "EmployeeName", employee.EmpNo);

and in Edit View
C#
<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>
Posted
Updated 8-Jul-13 23:17pm
v4
Comments
Jameel VM 8-Jul-13 1:34am    
can you elaborate your requirement?

1 solution

Create the Dropdownlist in the view and fill the datasource in your controller. It is the same as you have done for department name and Designation.
 
Share this answer
 
Comments
saurabh kumar mahto 9-Jul-13 1:57am    
Thanks Teenustar for your response,

I did this as below:

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);
ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "ReportingTo", employee.EmployeeID);

return View(employee);
}

//
// POST: /Employee/Edit/5

[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);
ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "ReportingTo", employee.EmployeeID);


return View(employee);
}

Edit View as below:

<div class="editor-label">
@Html.LabelFor(model => model.EmployeeID,"ReportingTo")
</div>
<div class="editor-field">
@Html.DropDownList("EmployeeID", String.Empty)
@Html.ValidationMessageFor(model => model.EmployeeID)
</div>

But getting error :
Unable to get value of the property 'call': object is null or undefined
Teenustar 9-Jul-13 10:16am    
When you debug, in the controller do you get values set in ViewBag.EmployeeID ?
Teenustar 9-Jul-13 10:17am    
Also it is a best practice to check for null reference errors

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900