0

I'm having trouble retrieving a list of my Departments and Buildings when I attempt to "Edit" an Employee. I'm able to retrieve the Employees First and Last Name but my DropDownList for Departments and Buildings on the Edit View of Employees are empty. Please see my Models, Controllers and Views below.

Employee Model:

public class Employee
{
    [Key]
    public int EmpId { get; set; }
    [Required]
    public string EmpFirstName { get; set; }
    [Required]
    public string EmpLastName { get; set; }

    public int DeptId { get; set; }
    public Department Department { get; set; }

    public int BldgId { get; set; }
    public Building Building { get; set; }
}

EmployeeController:

namespace DataEntryMVCEFCore.Controllers
{
    public class EmployeeController : Controller
    {
        private DataEntryContext _context;

        public EmployeeController(DataEntryContext context)
        {
            _context = context;
        }

        // GET: /<controller>/
        public IActionResult Index()
        {
            return View(_context.Employees.ToList());
        }

        // Populate Department values to DropDownList
        private IEnumerable<SelectListItem> GetDepartments()
        {
            return _context.Departments
                          .Select(s => new SelectListItem
                          {
                              Value = s.DeptId.ToString(),
                              Text = s.DeptTitle
                          })
                          .ToList();
        }

        // Populate Building values to DropDownList
        private IEnumerable<SelectListItem> GetBuildings()
        {
            return _context.Buildings
                          .Select(s => new SelectListItem
                          {
                              Value = s.BldgId.ToString(),
                              Text = s.BldgName
                          })
                          .ToList();
        }

        public IActionResult Create()
        {
            // Load values to DropDownLists for Departments and Buildings for Create Method
            ViewBag.DeptListName = GetDepartments();
            ViewBag.BldgListName = GetBuildings();

            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Create(Employee employee)
        {
            if (ModelState.IsValid)
            {
                _context.Employees.Add(employee);
                _context.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(employee);
        }

        public IActionResult Edit(int id)
        {
            var Employee = _context.Employees
                .Where(e => e.EmpId == id)
                .Single();
            return View(Employee);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Edit(Employee employee)
        {
            if (ModelState.IsValid)
            {
                _context.Employees.Update(employee);
                _context.SaveChanges();

                return RedirectToAction("Index");
            }

            return View(employee);
        }
    }
}

Employee Create View:

<form asp-controller="employee" asp-action="Create" method="post" class="form-horizontal" role="form">
<div class="form-horizontal">
    <div asp-validation-summary="All" class="text-danger"></div>
    <div class="form-group">
        <label asp-for="EmpFirstName" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="EmpFirstName" class="form-control" />
            <span asp-validation-for="EmpFirstName" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group">
        <label asp-for="EmpLastName" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="EmpLastName" class="form-control" />
            <span asp-validation-for="EmpLastName" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group">
        <label asp-for="DeptId" class="col-md-2 control-label"></label>
        <div class="col-md-10">      
            @*@Html.DropDownList("DeptList", ViewBag.DeptListName as SelectList, "Select Department")*@  
            <select asp-for="DeptId" asp-items="@ViewBag.DeptListName" class="form-control">
                <option>Please Select</option>
            </select>
            <span asp-validation-for="DeptId" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group">
        <label asp-for="BldgId" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            @*@Html.DropDownList("BldgList", ViewBag.BldgListName as SelectList, "Select Building")*@
            <select asp-for="BldgId" asp-items="@ViewBag.BldgListName" class="form-control">
                <option>Please Select</option>
            </select>
            <span asp-validation-for="BldgId" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>

Employee Edit View:

<form asp-controller="employee" asp-action="Edit" method="post" class="form-horizontal" role="form">
    <div class="form-horizontal">
        <div asp-validation-summary="All" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="EmpFirstName" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="EmpFirstName" class="form-control" />
                <span asp-validation-for="EmpFirstName" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group">
            <label asp-for="EmpLastName" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="EmpLastName" class="form-control" />
                <span asp-validation-for="EmpLastName" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group">
            <label asp-for="DeptId" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                @*@Html.DropDownList("DeptList", ViewBag.DeptListName as SelectList, "Select Department")*@
                <select asp-for="DeptId" asp-items="@ViewBag.DeptListName" class="form-control"></select>
                <span asp-validation-for="DeptId" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group">
            <label asp-for="BldgId" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                @*@Html.DropDownList("BldgList", ViewBag.BldgListName as SelectList, "Select Building")*@
                <select asp-for="BldgId" asp-items="@ViewBag.BldgListName" class="form-control"></select>
                <span asp-validation-for="BldgId" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
</form>
Lews Therin
  • 3,707
  • 2
  • 27
  • 53
Brian
  • 131
  • 1
  • 7
  • 16
  • http://stackoverflow.com/questions/18382311/populating-a-razor-dropdownlist-from-a-listobject-in-mvc – Larry Jul 08 '16 at 17:37

1 Answers1

0

Your view is using ViewBag,You forgot to set the items to ViewBag.DeptListName and ViewBag.BldgListName to render the dropdowns. But in your edit action method you are not setting those ( you did that in create method).

public IActionResult Edit(int id)
{
   var emp = _context.Employees.Where(e => e.EmpId == id).FirstOrDefault();
   if(emp==null)
       return View("NotFound"); //make sure you have this view.

   ViewBag.DeptListName = GetDepartments();
   ViewBag.BldgListName = GetBuildings();
   return View(emp );
}
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Hi Shyju, Thank you for replying back.. I implemented what you mentioned and I was able to retrieve the data to the DropDownLists along with the proper selected value in the DropDownLists for Departments and Buildings. However, when I try to update the DropDownList values or even the First and Last names I get the following error below: – Brian Jul 08 '16 at 17:47
  • An exception of type 'Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code Additional information: Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions. – Brian Jul 08 '16 at 17:48
  • That error has nothing to do with your original question or the answer i provided for that. Read the exception details and you should be able to figure it out. – Shyju Jul 08 '16 at 17:49
  • I must be overlooking this error or over thinking it because I'm having a heck of a time finding the issue as to why I can't update . – Brian Jul 08 '16 at 18:21
  • Hi Shyju, Sorry to ask this, but can you point out to me why I'm unable to update? I see the DbUpdateConcurrencyException but not sure how to trouble shoot that better to resolve the Updates properly. – Brian Jul 08 '16 at 19:24
  • Do you have a hidden field in your edit form for the Employee record ? ( EmployeeId ?) – Shyju Jul 08 '16 at 19:26
  • Not that I can tell. I see fields for EmpFirstName, EmpLastName, DeptID and BldgId. I should not have one for EmpId, correct? – Brian Jul 08 '16 at 19:40
  • Keep `EmpId` in a hidden field in the form. – Shyju Jul 08 '16 at 19:42
  • If you don't mind answering this for me, is there a performance decrease or security concern(s) for using ViewBag or ViewData? – Brian Jul 08 '16 at 19:59
  • I do not believe there is any signifcant performance/security issues. You need to cast the viewbag properties to appropriate types as needed. I personally prefer strongly typed than dynamic. – Shyju Jul 08 '16 at 20:01