示例#1
0
        public ActionResult Index()
        {
            var trips     = _repo.GetTrips();
            var viewModel = new TripViewModels();

            viewModel.NewTrip = new Trip();
            viewModel.Trips   = trips;
            if (TempData["Success"] != null)
            {
                ViewBag.Success = TempData["Success"];
            }
            if (TempData["WarningMessage"] != null)
            {
                ViewBag.WarningMessage = TempData["WarningMessage"];
            }
            return(View(viewModel));
        }
示例#2
0
        public async Task <IActionResult> Post([FromBody] TripViewModels trip)
        {
            if (ModelState.IsValid)
            {
                var newTrip = Mapper.Map <Trip>(trip);

                _repository.AddTrip(newTrip);

                if (await _repository.SaveChangesAsync())
                {
                    // Returning the TripViewModel and not the entity itself (Trip) for security/encapsulation.
                    return(Created($"api/trips/{trip.Name}", Mapper.Map <TripViewModels>(newTrip)));
                }
            }


            // DEBUGGING: Determine why ModelState is invalid:
            //var errors = ModelState.Values.SelectMany(v => v.Errors);


            // Returning just the ModelState is helpful for debugging in internal services.
            return(BadRequest("Failed to save the trip."));
        }