0

我有 3 个与外键 Student、Loan 和 Book 相关的表,当我创建一个新学生时,我可以将它与 book 表 Book 相关联,并将其保存在表 Loan 中,但是如果我在现有学生中进行编辑,则更改不会保存在 Student 中桌子。

StudentController.cs 我

        public ActionResult Create()
    {
        ViewBag.ISBN = new SelectList(db.Books, "ISBN", "Titulli");
        return View();
    }

    //
    // POST: /Student/Create

    [HttpPost]
    public ActionResult Create(Student student)
    {
        if (ModelState.IsValid)
        {
            db.Students.Add(student);
            db.SaveChanges();
            Loan l = new Loan()
            {
                StudentID = student.StudentID,
                ISBN = student.ISBN.Value,
            };
            db.Loans.Add(l);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.ISBN = new SelectList(db.Books, "ISBN", "Titulli", student.ISBN);

        return View(student);
    }

    //
    // GET: /Student/Edit/5

    public ActionResult Edit(int id = 0)
    {

        Student student = db.Students.Find(id);
        if (student == null)
        {
            return HttpNotFound();
        }

        ViewBag.ISBN1 = new SelectList(db.Books, "ISBN", "Titulli");
        return View(student);
    }

    //
    // POST: /Student/Edit/5

    [HttpPost]
    public ActionResult Edit(Student student)
    {
        if (ModelState.IsValid)
        {
            db.Entry(student).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.ISBN1 = new SelectList(db.Books, "ISBN", "Titulli", student.ISBN);
        return View(student);
    }

如果我将代码预先保存在 Loan 中,在这种情况下我会收到错误消息,说“可空对象必须有一个值”。

StudentController.cs II

public ActionResult Create()
    {
        ViewBag.ISBN = new SelectList(db.Books, "ISBN", "Titulli");
        return View();
    }

    //
    // POST: /Student/Create

    [HttpPost]
    public ActionResult Create(Student student)
    {
        if (ModelState.IsValid)
        {
            db.Students.Add(student);
            db.SaveChanges();
            Loan l = new Loan()
            {
                StudentID = student.StudentID,
                ISBN = student.ISBN.Value,
            };
            db.Loans.Add(l);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.ISBN = new SelectList(db.Books, "ISBN", "Titulli", student.ISBN);

        return View(student);
    }

    //
    // GET: /Student/Edit/5

    public ActionResult Edit(int id = 0)
    {

        Student student = db.Students.Find(id);
        if (student == null)
        {
            return HttpNotFound();
        }

        ViewBag.ISBN1 = new SelectList(db.Books, "ISBN", "Titulli");
        return View(student);
    }

    //
    // POST: /Student/Edit/5

    [HttpPost]
    public ActionResult Edit(Student student)
    {
        if (ModelState.IsValid)
        {
            db.Entry(student).State = EntityState.Modified;
            db.SaveChanges();
            Loan w = new Loan()
            {
                StudentID = student.StudentID,
                ISBN = student.ISBN.Value,
            };
            db.Loans.Add(w);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.ISBN1 = new SelectList(db.Books, "ISBN", "Titulli", student.ISBN);
        return View(student);
    }
4

1 回答 1

0

尝试这个:

[HttpPost]
public ActionResult Edit(Student student)
{
    if (ModelState.IsValid)
    {
        var studentDB = db.Students.Find(id);
        db.Entry(studentDB).CurrentValues.SetValues(student);

        db.SaveChanges();
        Loan w = new Loan()
        {
            StudentID = student.StudentID,
            ISBN = student.ISBN.Value,
        };
        db.Loans.Add(w);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    ViewBag.ISBN1 = new SelectList(db.Books, "ISBN", "Titulli", student.ISBN);
    return View(student);
}
于 2012-12-26T20:00:57.677 回答