具有键“JobTitleID”的 ViewData 项的类型为“System.Int32”,但必须为“IEnumerab

本文关键字:Int32 IEnumerab System JobTitleID ViewData 类型 | 更新日期: 2023-09-27 18:35:14

遵循教程,并逐步遵循它,但是当我想编辑字段时出现错误。

源错误:

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

这是我的控制器:

// 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);
    }

我对MVC不是很熟悉,只是想问问问题是什么,以及如何解决它。

具有键“JobTitleID”的 ViewData 项的类型为“System.Int32”,但必须为“IEnumerab

ViewBag

内部封装ViewData,因此,您收到的消息是关于 ViewBag 的。您已在控制器上设置了ViewBag.JobID,并且在您的视图中,您正在尝试使用另一个键 JobTitleID 。尝试使用正确的键:

@Html.DropDownList("JobID", String.Empty)
相关文章: