Click here to Skip to main content
14,270,121 members
Rate this:
Please Sign up or sign in to vote.
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
// 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
<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
<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

Rate this:
Please Sign up or sign in to vote.

Solution 1

Create a class like below
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
ViewBag.EmployeeId =
                DropDownList<Employee>.LoadItems(
                    db.Employees, "EmpId", "Name");



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

Hope this helps
   
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()
saurabh kumar mahto 1-Aug-13 8:52am
   
I really wonder why all these are not working !!
Still its not saving in the database ??
Jameel VM 1-Aug-13 10:10am
   
now what's the pblm? did you got the id in the action?
Jameel VM 1-Aug-13 10:12am
   
please make sure that the view return type should be the type Employee. like @model Employee

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100