在ASP.NET MVC中更新相关数据4

时间:2012-12-26 19:34:48

标签: c# asp.net-mvc database entity-framework database-first

我有3个与外键学生,贷款和书籍相关的表格,当我创建一个新学生时,我可以将它与书籍表格书籍相关联并将其保存在表格贷款中,但如果我在现有学生中进行编辑更改不是保存在学生表中。

StudentController.cs I

        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中,那么我也没有错误说“Nullable对象必须有值。”

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

1 个答案:

答案 0 :(得分: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);
}