Exemplo n.º 1
0
        public ActionResult Delete(Rental rentalToDelete)
        {
            if (ModelState.IsValid)
            {
                using (var db = new RentalDBContext())
                {
                    // already have the Rental : Rental rentalToDelete  = db.Rentals.Find(id);

                    var rentalTenants = db.Tenants.Where(rt => rt.RentalID == rentalToDelete.Id).AsEnumerable();

                    foreach (var rt in rentalTenants)
                    {
                        var singleRt = rt;
                        // this just detaches?  db.Tenants.Remove(singleRt);
                        db.Entry(singleRt).State = System.Data.Entity.EntityState.Deleted;
                    }

                    // db.Entry(rentalToDelete).State = System.Data.Entity.EntityState.Deleted;
                    //db.Rentals.Remove(rentalToDelete);
                    db.Rentals.Attach(rentalToDelete);
                    db.Rentals.Remove(rentalToDelete);

                    db.SaveChanges();
                }
                return(RedirectToAction("Index"));
            }
            return(View(rentalToDelete));
        }
Exemplo n.º 2
0
 public ActionResult Add(Tenant tenantFromView)
 {
     if (ModelState.IsValid)
     {
         using (var db = new RentalDBContext())
         {
             db.Tenants.Add(tenantFromView);
             db.SaveChanges();
         }
         return(RedirectToAction("Index", "Tenant", new { id = tenantFromView.RentalID }));
     }
     return(View(tenantFromView));
 }
Exemplo n.º 3
0
 public ActionResult Add(Rental rentalFromView)
 {
     if (ModelState.IsValid)
     {
         using (var db = new RentalDBContext())
         {
             db.Rentals.Add(rentalFromView);
             db.SaveChanges();
         }
         return(RedirectToAction("Index"));
     }
     return(View(rentalFromView));
 }
Exemplo n.º 4
0
        public ActionResult Edit(Tenant tenantToUpdate)
        {
            if (ModelState.IsValid)
            {
                using (var db = new RentalDBContext())
                {
                    db.Entry(tenantToUpdate).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }

                return(RedirectToAction("Index", "Tenant", new { id = tenantToUpdate.RentalID }));
            }
            return(View(tenantToUpdate));
        }
Exemplo n.º 5
0
        public ActionResult Edit(Rental rentalToUpdate)
        {
            if (ModelState.IsValid)
            {
                using (var db = new RentalDBContext())
                {
                    db.Entry(rentalToUpdate).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }

                return(RedirectToAction("Index"));
            }
            return(View(rentalToUpdate));
        }