public Distance(double distance, DistanceUnits units) { var calculator = new DistanceCalculator(); var distanceData = new DistanceData { DistanceM = distance, DistanceKm = distance }; switch (units) { case DistanceUnits.Miles: distanceData.Calc = "kilometers"; break; case DistanceUnits.Kilometers: distanceData.Calc = "miles"; break; default: throw new ArgumentException("Unknown units: " + units, "units"); } calculator.Calculate(distanceData); DistanceInM = distanceData.DistanceM.Value; DistanceInKm = distanceData.DistanceKm.Value; BaseDistance = distance; BaseUnits = units; }
public ActionResult CalcDistance(DistanceData distanceCalculation) { if (!ModelState.IsValid) return new JsonResult { Data = distanceCalculation }; distanceCalc.Calculate(distanceCalculation); return new JsonResult { Data = distanceCalculation }; }