public ActionResult New(string licensePlate) { Car car = CarRepository.RetrieveByLicensePlate(licensePlate); List<Refuelling> refuellings = RefuellingRepository.List(car); refuellings.Reverse(); Refuelling refuelling = new Refuelling(); refuelling.Date = DateTime.Now; var viewData = new NewRefuellingViewData { Car = car, LastRefuelling = refuellings.Count > 0 ? refuellings[0] : null, NewRefuelling = refuelling }; return View("New", viewData); }
public ActionResult New(string licensePlate, FormCollection form) { Car car = CarRepository.RetrieveByLicensePlate(licensePlate); Refuelling refuelling = null; try { refuelling = new Refuelling(); refuelling.Car = car; this.UpdateModel(refuelling, new[] { "Date", "ServiceStation", "Kilometers", "Liters", "PricePerLiter", "Total", "Usage" }); RefuellingRepository.Add(refuelling); return RedirectToAction("List", new { licensePlate = licensePlate }); } catch (RuleViolationException) { List<Refuelling> refuellings = RefuellingRepository.List(car); refuellings.Reverse(); this.UpdateModelStateWithViolations(refuelling, ViewData.ModelState); var viewData = new NewRefuellingViewData { Car = car, LastRefuelling = refuellings.Count > 0 ? refuellings[0] : null, NewRefuelling = refuelling }; return View("New", viewData); } }