0

I have 3 tables related with foreign keys Student, Loan and Book, when I create a new student I can relate it with book form table Book and save it also in table Loan, but if I edit in existing student changes are not saved in Student table.

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

Neither if i put code fore saving it in Loan, in this case i get error saying "Nullable object must have a value."

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);
    }
ilirvg
  • 193
  • 1
  • 2
  • 8
  • Have you tried search for the Student? Sample: db.Single(s=>s.StudentID == student.StudentID); before set the new values? – Thiago Custodio Dec 26 '12 at 19:40
  • I think i have tried in this line if i am correct ? Student student = db.Students.Find(id) – ilirvg Dec 26 '12 at 19:55
  • The answer may lie in db.GetValidationErrors(); – phil soady Dec 26 '12 at 19:57
  • Yes, you should do that before this lines: [HttpPost] public ActionResult Edit(Student student) { if (ModelState.IsValid) { Student student = db.Students.Find(id) db.Entry(student).State = EntityState.Modified; db.SaveChanges(); //...continue – Thiago Custodio Dec 26 '12 at 19:58
  • It's the same think again but thank u for your time. – ilirvg Dec 26 '12 at 20:04

1 Answers1

0

Try this:

[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);
}
Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90