Ejemplo n.º 1
0
        // GET: Tourists/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Tourist tourist = db.Tourists.Find(id);

            if (tourist == null)
            {
                return(HttpNotFound());
            }


            var Results = from f in db.Flights
                          select new
            {
                f.FlightId,
                f.DepartureDate,
                f.Name,
                Checked = ((from tf in db.TouristsToFlights
                            where (tf.TouristId == id) & (tf.FlightId == f.FlightId)
                            select tf).Count() > 0)
            };
            var MyViewModel = new TouristsViewModel();

            MyViewModel.TouristId   = id.Value;
            MyViewModel.FirstName   = tourist.FirstName;
            MyViewModel.LastName    = tourist.LastName;
            MyViewModel.Gender      = tourist.Gender;
            MyViewModel.Country     = tourist.Country;
            MyViewModel.Remarks     = tourist.Remarks;
            MyViewModel.DateOfBirth = tourist.DateOfBirth;

            var MyCheckBoxList = new List <CheckBoxViewModel>();

            foreach (var item in Results)
            {
                MyCheckBoxList.Add(new CheckBoxViewModel
                {
                    Id            = item.FlightId,
                    Name          = item.Name,
                    Checked       = item.Checked,
                    DepartureDate = item.DepartureDate
                });
            }

            MyViewModel.Flights = MyCheckBoxList;

            return(View(MyViewModel));
        }
Ejemplo n.º 2
0
        public ActionResult Edit(TouristsViewModel tourist)
        {
            if (ModelState.IsValid)
            {
                var myTourist = db.Tourists.Find(tourist.TouristId);

                myTourist.FirstName = tourist.FirstName;
                myTourist.LastName  = tourist.LastName;

                foreach (var item in db.TouristsToFlights)
                {
                    if (item.TouristId == tourist.TouristId)
                    {
                        db.Entry(item).State = System.Data.Entity.EntityState.Deleted;
                    }
                }

                foreach (var item in tourist.Flights)
                {
                    foreach (var item2 in db.Flights.Where(n => n.FlightId == item.Id))
                    {
                        if (item.Checked && item2.NumberOfSeats > item2.NumberOfSeatsTaken)
                        {
                            db.TouristsToFlights.Add(new TouristToFlight()
                            {
                                TouristId = tourist.TouristId,
                                FlightId  = item.Id
                            });
                            item2.NumberOfSeatsTaken += 1;
                        }
                    }
                }
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(tourist));
        }