Exemplo n.º 1
0
        // GET: Owners/AddOwner/5
        public ActionResult AddOwner(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            CarsOwner carowner = new CarsOwner();

            carowner.cars = db.car.Find(id);

            if (carowner.cars == null)
            {
                return(HttpNotFound());
            }

            if (carowner.cars.ownerID != null)
            {
                return(RedirectToAction("CarOwned", "Cars"));
            }

            return(View(carowner));
        }
Exemplo n.º 2
0
        public ActionResult AddOwner(CarsOwner carsowner)
        {
            if (ModelState.IsValid)
            {
                var entity = new Owner()
                {
                    ownerFirstName = carsowner.owner.ownerFirstName, ownerLastName = carsowner.owner.ownerLastName
                };

                db.owner.Add(entity);

                db.SaveChanges();

                int newOwner = (int)entity.ownerID;

                using (db)
                {
                    // make sure you have the right column/variable used here
                    var cars = db.car.FirstOrDefault(x => x.carID == carsowner.cars.carID);

                    if (cars == null)
                    {
                        throw new Exception("Invalid id: " + carsowner.cars.carID);
                    }

                    // this variable is tracked by the db context
                    cars.ownerID = newOwner;

                    db.SaveChanges();
                }

                return(RedirectToAction("Index"));
            }

            return(View(carsowner));
        }