1

Following a tutorial, and have followed it step-by-step but get an error when I want to edit a field.

Source Error:

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

Here's my controller:

// POST: /Employee/Create

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "SIN, FirstName, MiddleName, LastName, StartDate, Salary, JobTitleID, DepartmentID")] Employee employee)
    {
        try
        {

            if (ModelState.IsValid)
            {
                db.Employees.Add(employee);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
        }
        catch (DataException dex)
        {
            if (dex.InnerException.InnerException.Message.Contains("IX_Employee_SIN"))
            {
                ModelState.AddModelError("SIN", "Unable to save changes. Remember, you cannot have duplicate SIN numbers.");
            }
            else
            {
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }
        }

        DepartmentDropDownList(employee.DepartmentID);
        JobDropDownList(employee.JobTitleID);
        return View(employee);
    }

    //
    // GET: /Employee/Edit/5

    public ActionResult Edit(int id = 0)
    {
        Employee employee = db.Employees.Find(id);
        if (employee == null)
        {
            return HttpNotFound();
        }
        DepartmentDropDownList(employee.DepartmentID);
        JobDropDownList(employee.JobTitleID);
        return View(employee);
    }

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

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "ID, SIN, FirstName, MiddleName, LastName, StartDate, Salary, JobTitleID, DepartmentID")] Employee employee)
    {
        try
        {

            if (ModelState.IsValid)
            {
                db.Employees.Add(employee);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

        }
        catch (DataException dex)
        {
            if (dex.InnerException.InnerException.Message.Contains("IX_Employee_SIN"))
            {
                ModelState.AddModelError("SIN", "Unable to save changes. Remember, you cannot have duplicate SIN numbers.");
            }
            else
            {
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }
        }

        DepartmentDropDownList(employee.DepartmentID);
        JobDropDownList(employee.JobTitleID);
        return View(employee);
    }


   private void JobDropDownList(object selectedJob = null)
    {
        var dQuery = from d in db.JobTitles
                     orderby d.Title
                     select d;
        ViewBag.JobID = new SelectList(dQuery, "ID", "Title", selectedJob);
    }
    private void DepartmentDropDownList(object selectedDepartment = null)
    {
        var dQuery = from d in db.Departments
                     orderby d.DepartmentName
                     select d;
        ViewBag.DepartmentID = new SelectList(dQuery, "ID", "DepartmentName",selectedDepartment);
    }

I'm not really that familiar with MVC, just thought I'd ask what the issue is, and how it can be fixed.

1011 1110
  • 743
  • 2
  • 17
  • 43
  • Which version of MVC are you using? It looks like you're trying to use this extension method: http://msdn.microsoft.com/en-us/library/dd504970(v=vs.108).aspx - perhaps it was introduced in a version later than the one you're using? – Jon Skeet Oct 14 '13 at 18:39
  • The error suggests that you are trying to bind an integer to a dropdown list which is expecting a IEnumerable of select list items – Adween Oct 14 '13 at 18:44
  • It's not clear what your `DepartmentDropDownList` and `JobDropDownList` methods do, btw - that may be the problem... – Jon Skeet Oct 14 '13 at 18:44
  • try `@Html.DropDownList("JobID", String.Empty)` – Adween Oct 14 '13 at 18:51
  • Trying to order them alphabetically. – 1011 1110 Oct 14 '13 at 18:51
  • this might help, http://stackoverflow.com/questions/9642821/mvc3-dropdownlist-viewbag-issue – Adween Oct 14 '13 at 18:55

1 Answers1

0

The ViewBag encapsulate the ViewData internally, so, the message you are getting is about the ViewBag. You have setted the ViewBag.JobID on the controller and in your view you are trying to use another key, JobTitleID. Try using the right key:

@Html.DropDownList("JobID", String.Empty)
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194