Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an Employee table with following Columns:

EmployeeId,EmployeeName,DeptID,DesID,ReportingTo

In Edit View,I want ReportingTo Column should get populated with DropDownList items of
EmployeeName from the same Table.

I am able to do the selection on DropDownList, but the selected Item is not getting saved into database,hence its showing Null value.

How to save the selected item ?

Thank You,

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

           return View(employee);
       }

In Edit.cshtml
C#
<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>


In Index.cshtml
C#
<td>
    @Html.DisplayFor(modelItem => item.EmployeeName)
</td>
<td>
    @Html.DisplayFor(modelItem => item.EmpTypeID)
</td>
<td>
    @Html.DisplayFor(modelItem => item.Department.DeptShortDesc)
</td>
<td>
    @Html.DisplayFor(modelItem => item.Designation.ShortDesc)
</td>
<td>
    @Html.DisplayFor(modelItem => item.EmployeeName,"ReportingTo")
</td>
Posted

1 solution

Create a class like below
C#
public static class DropDownList<T>
   {
       public static SelectList LoadItems(IList<T> collection, string value, string text)
       {
           return new SelectList(collection, value, text);
       }

   }


Set the employee in your Edit acion result
C#
ViewBag.EmployeeId =
                DropDownList<Employee>.LoadItems(
                    db.Employees, "EmpId", "Name");



View code
C#
@Html.DropDownListFor(model => model.EmployeeID, (IEnumerable<SelectListItem>)ViewBag.EmployeeId, new { @class = "select")

Hope this helps
 
Share this answer
 
v2
Comments
saurabh kumar mahto 1-Aug-13 4:43am    
Thanks Jameel,
I tried with your solution. changed in Edit.cshtml
from @Html.DropDownList("EmployeeID",string.Empty) To your solution
But no change in output.still the same ???
Am I correct ?
Jameel VM 1-Aug-13 5:35am    
i have update my answer.please try that
Jameel VM 1-Aug-13 5:38am    
Please make sure that the property id should be same as both in the view and Action. For example you should use parameter like Edit(int EmployeeID)
saurabh kumar mahto 1-Aug-13 7:21am    
I tried with your solution and i am geeting an compiler error as below,

The best overloaded match for 'LMS2.controllers.dropDownList<lms2.models.employee>.LoadItems(System.Collection.Generic.IList<lms2.models.employee>,string,string)' has some invalid arguments.

I have made the class DropDownList<t> in the EmployeeController itself
Jameel VM 1-Aug-13 7:34am    
convert your collection to list. for example db.Employees.ToList()

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