1

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

源错误:

     </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 不是很熟悉,只是想问一下问题是什么,以及如何解决。

4

1 回答 1

0

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

@Html.DropDownList("JobID", String.Empty)
于 2013-10-14T18:55:47.387 回答