I have a problem concerning the following code snippet for my university project. The system this is implemented in is a library for books - nothing too special as i am new to EF and MVC. The following is the [Http-Post] Method of creating a lending. When a lending is created, i want to make sure all reserves concerning the same User and the same Medium will be marked as served so they won't play a role for the next lending.
My problem is, that it won't save the changes the reserve-item. Does anybody see a reason why? It's not the only place i change values, but i'm not able to find out whats different here.
Model
public partial class Reserve
{
public int Id { get; set; }
public System.DateTime ReserveDate { get; set; }
public string User { get; set; }
public bool Served { get; set; }
public virtual Medium Medium { get; set; }
}
Code
public ActionResult Create([Bind(Include = "Id,LendingDate,ExpiryDate,ReturnDate,User,Copy")] Lending lending, int? Copy_ID)
{
lending.Copy = db.CopySet.Find(Copy_ID);
lending.LendingDate = DateTime.Today;
lending.ExpiryDate = lending.LendingDate.AddDays(DaysToExpiry);
lending.ReturnDate = null;
if (ModelState.IsValid)
{
//wenn ausleihe erfolgt, die Reservierung finden, die zu Benutzer und Medium passt
List<Reserve> resList = db.ReserveSet.Where(x => x.User.Contains(lending.User) && x.Medium.Id.Equals(lending.Copy.Medium.Id)).ToList();
//für alle Elemente
foreach (Reserve r in resList)
{
Reserve res = db.ReserveSet.Find(r.Id);
res.Served = true;
db.Entry(res).State = EntityState.Modified;
db.SaveChanges();
}
db.LendingSet.Add(lending);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(lending);
}