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