//
        // GET: /Trip/Create
        public ActionResult Create()
        {
            Trip newTrip = new Trip();
            newTrip.Persons = new List<Person>();
            ViewBag.PossibleTransportTypes = _repository.List<TransportType>();
            ViewBag.PossibleTrails = _repository.List<Trail>();
            PopulatePersons();

            return View(newTrip);
        }
        public ActionResult Create(Trip trip, string personidlist)
        {
            try
            {
                if (ModelState.IsValid)
                {

                    TransportType transportType = _repository.GetById<TransportType>(trip.TransportTypeId);
                    trip.TransportType = transportType;
                    Trail trail = _repository.GetById<Trail>(trip.TrailId);
                    trip.Trail = trail;

                    _repository.Insert(trip);

                    if (!string.IsNullOrEmpty(personidlist))
                    {
                        if (trip.Persons == null)
                            trip.Persons = new List<Person>();
                        string[] personIdArray = personidlist.Split(new[] {','});
                        foreach (string value in personIdArray)
                        {
                            int personId = Convert.ToInt32(value);
                            Person person = _repository.GetById<Person>(personId);
                            if(person != null)
                            {
                                trip.Persons.Add(person);
                            }
                        }
                    }

                    _repository.SaveChanges();
                    return RedirectToAction("Index");
                }

                ViewBag.PossibleTransportTypes = _repository.List<TransportType>();
                ViewBag.PossibleTrails = _repository.List<Trail>();
                PopulatePersons();
                return View();

            }
            catch
            {
                ViewBag.PossibleTransportTypes = _repository.List<TransportType>();
                ViewBag.PossibleTrails = _repository.List<Trail>();
                PopulatePersons();
                return View();
            }
        }
Exemple #3
0
        public void ShouldAddNewTrip()
        {
            // To turn on DTS on my computer,
            // see http://alexduggleby.com/2008/08/24/msdtc-unavailable-for-sql-express-transactions-or-who-took-my-msdtc-settings-on-vista/
            // or http://geekswithblogs.net/narent/archive/2006/10/09/93544.aspx
               using (new TransactionScope())
            {
                using (TrailsContext context = new TrailsContext())
                {
                    DbSet<Trip> trips = context.Trips;
                    trips.Load();
                    int priorCount = trips.Local.Count;

                    DbSet<Person> persons = context.Persons;
                    persons.Load();

                    Trip newTrip = new Trip
                    {
                        Date = DateTime.Today,
                        TimeTaken = 3.1M,
                        TransportTypeId = 1,
                        TrailId = 1,
                        Persons = new List<Person>{persons.Local.ElementAt(0), persons.Local.ElementAt(2)}
                    };

                    // Add the new trip to the context
                    Trip trip = trips.Add(newTrip);

                    // Save the context to the database, causing the database to be updated
                    context.SaveChanges();
                    // At this stage, the database has the new row, so the identity has been incremented and assigned
                    Assert.IsTrue(trip.Id > 0);
                    Assert.IsTrue(newTrip.Id > 0);

                    trips.Load();
                    int postCount = trips.Local.Count;

                    Assert.AreEqual(postCount, priorCount + 1);
                }
                // Rollback the transaction, to make the test repeatable
            }
        }
        public ActionResult Edit(int id, FormCollection collection, Trip trip, List<Person> Persons)
        {
            try
            {
                Trip savedTrip = _repository.GetById<Trip>(trip.Id);
                if (ModelState.IsValid)
                {
                    UpdateModel(savedTrip);

                    if (trip.TransportType == null || trip.TransportType.Id != trip.TransportTypeId)
                    {
                        TransportType transportType = _repository.GetById<TransportType>(trip.TransportTypeId);
                        trip.TransportType = transportType;
                    }
                    if (trip.Trail == null || trip.Trail.Id != trip.TrailId)
                    {
                        Trail trail = _repository.GetById<Trail>(trip.TrailId);
                        trip.Trail = trail;
                    }

                    _repository.Update(savedTrip);
                    _repository.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    ViewBag.PossibleTransportTypes = _repository.List<TransportType>();
                    ViewBag.PossibleTrails = _repository.List<Trail>();
                    PopulatePersons();
                    return View();
                }
            }
            catch
            {
                ViewBag.PossibleTransportTypes = _repository.List<TransportType>();
                ViewBag.PossibleTrails = _repository.List<Trail>();
                PopulatePersons();
                return View();
            }
        }