public ActionResult AddMonthlyPrices(MonthlyPricesViewModel mpVM) { if (!Request.IsAuthenticated) { return(RedirectToAction("Login", "Account")); } else { if (ModelState.IsValid) { //send a request on the main server and await for the response DTOHelper dtoHlp = new DTOHelper(); MyRemoteServices.AgentEndpointPortClient aepc = new MyRemoteServices.AgentEndpointPortClient(); MyRemoteServices.manageMonthlyPricesRequest ampRequest = dtoHlp.GetMonthlyPricesRequest(mpVM); MyRemoteServices.manageMonthlyPricesResponse ampResponse = aepc.manageMonthlyPrices(ampRequest); bool isUpdate = false; if (ampResponse.responseWrapper.sucess) { //save localy using (var ctx = new ApplicationDbContext()) { //see if the monthly price sheet already exists var queryData = ctx.MonthlyPrices .Include("BookingUnit") .Where(x => x.BookingUnit.Id == mpVM.BookingUnitId && x.Year == mpVM.Year) .OrderBy(x => x.Month) .ToList(); if (queryData.Count == 0) { //if not, add a new one BookingUnit myUnit = ctx.BookingUnits.FirstOrDefault(x => x.Id == mpVM.BookingUnitId); //help structure for monthly prices double[] myMonths = new double[12]; myMonths[0] = mpVM.JanuaryPrice; myMonths[1] = mpVM.FebruaryPrice; myMonths[2] = mpVM.MarchPrice; myMonths[3] = mpVM.AprilPrice; myMonths[4] = mpVM.MayPrice; myMonths[5] = mpVM.JunePrice; myMonths[6] = mpVM.JulyPrice; myMonths[7] = mpVM.AugustPrice; myMonths[8] = mpVM.SeptemberPrice; myMonths[9] = mpVM.OctoberPrice; myMonths[10] = mpVM.NovemberPrice; myMonths[11] = mpVM.DecemberPrice; //long[] myMainServedIds = (long[])ampResponse.responseWrapper.mainServerId; for (int i = 0; i < 12; i++) { MonthlyPrices newMp = new MonthlyPrices { Year = mpVM.Year, Month = i + 1, BookingUnit = myUnit, Amount = myMonths[i], MainServerId = 1 }; ctx.MonthlyPrices.Add(newMp); } } else { //if yes, edit the existing one foreach (var m in queryData) { if (m.Month == 1) { m.Amount = mpVM.JanuaryPrice; } if (m.Month == 2) { m.Amount = mpVM.FebruaryPrice; } if (m.Month == 3) { m.Amount = mpVM.MarchPrice; } if (m.Month == 4) { m.Amount = mpVM.AprilPrice; } if (m.Month == 5) { m.Amount = mpVM.MayPrice; } if (m.Month == 6) { m.Amount = mpVM.JunePrice; } if (m.Month == 7) { m.Amount = mpVM.JulyPrice; } if (m.Month == 8) { m.Amount = mpVM.AugustPrice; } if (m.Month == 9) { m.Amount = mpVM.SeptemberPrice; } if (m.Month == 10) { m.Amount = mpVM.OctoberPrice; } if (m.Month == 11) { m.Amount = mpVM.NovemberPrice; } if (m.Month == 12) { m.Amount = mpVM.DecemberPrice; } } isUpdate = true; } //save changes ctx.SaveChanges(); } } else { //some error happened, retry TempData["error"] = ampResponse.responseWrapper.message; return(RedirectToAction("ManageMonhtlyPrices", "Agent", new { bookingUnitId = mpVM.BookingUnitId })); } TempData["success"] = "Successfully ADDED a new monthly prices sheet for the year " + mpVM.Year; if (isUpdate) { TempData["success"] = "Successfully UPDATED the monthly prices sheet for the year " + mpVM.Year; } return(RedirectToAction("AgentPage", "Agent")); } else { //invalid VM exception TempData["error"] = "Some form atributes are incorrect"; return(RedirectToAction("ManageMonhtlyPrices", "Agent", new { bookingUnitId = mpVM.BookingUnitId })); } } }