public ActionResult AddBookingUnit(AddNewBookingUnitViewModel anbuVM) { if (!Request.IsAuthenticated) { return(RedirectToAction("Login", "Account")); } else { if (ModelState.IsValid) { DTOHelper dtoHlp = new DTOHelper(); bool imgValidation = dtoHlp.ValidateImageTypes(anbuVM.Images); if (imgValidation) { List <byte[]> imagesCopy = new List <byte[]>(); foreach (var img in anbuVM.Images) { MemoryStream target = new MemoryStream(); img.InputStream.CopyTo(target); byte[] data = target.ToArray(); imagesCopy.Add(data); } long curentUserId = User.Identity.GetUserId <long>(); //send a request on the main server and await for the response MyRemoteServices.AgentEndpointPortClient aepc = new MyRemoteServices.AgentEndpointPortClient(); MyRemoteServices.addBookingUnitRequest abuRequest = dtoHlp.GetBookingUnitRequest(anbuVM, curentUserId, imagesCopy); MyRemoteServices.addBookingUnitResponse abuResponse = aepc.addBookingUnit(abuRequest); if (abuResponse.responseWrapper.success) { //save localy using (var ctx = new ApplicationDbContext()) { //create a new booking unit with all the prerequisits City myCity = ctx.Cities.FirstOrDefault(x => x.Id.ToString() == anbuVM.CityId); ApplicationUser agentUsr = ctx.Users.FirstOrDefault(x => x.Id == curentUserId); AccomodationType myAccType = ctx.AccomodationTypes.FirstOrDefault(x => x.Id.ToString() == anbuVM.AccomodationTypeId); AccomodationCategory myAccCat = ctx.AccomodationCategories.FirstOrDefault(x => x.Id.ToString() == anbuVM.AccomodationCategoryId); ICollection <BonusFeatures> myBonusFeatures = new List <BonusFeatures>(); if (anbuVM.BonusFeatures != null) { foreach (var bfvm in anbuVM.BonusFeatures) { if (bfvm.IsSelected) { var myBFeature = ctx.BonusFeatures.FirstOrDefault(x => x.Id == bfvm.Id); if (myBFeature != null) { myBonusFeatures.Add(myBFeature); } } } } BookingUnit newUnit = new BookingUnit { Name = anbuVM.Name, Address = anbuVM.Address, City = myCity, Description = anbuVM.Description, PeopleNo = anbuVM.PeopleNo, Agent = agentUsr, AccomodationType = myAccType, AccomodationCategory = myAccCat, BonusFeatures = myBonusFeatures, MainServerId = (long?)abuResponse.responseWrapper.responseBody }; //add the new unit to the DBContext ctx.BookingUnits.Add(newUnit); //get uploaded images and save them on the server var uploadDir = "~/uploadedImages"; var cnt = 0; foreach (var imgUpl in anbuVM.Images) { string newFileName = string.Concat(Path.GetFileNameWithoutExtension(imgUpl.FileName) , DateTime.Now.ToString("_yyyy_MM_dd_HH_mm_ss") , Path.GetExtension(imgUpl.FileName) ); var imagePath = Path.Combine(Server.MapPath(uploadDir), newFileName); System.IO.File.WriteAllBytes(imagePath, imagesCopy[cnt].ToArray()); var imageUrl = Path.Combine(uploadDir, newFileName); BookingUnitPicture newPicture = new BookingUnitPicture { BookingUnit = newUnit, Value = imageUrl }; ctx.Pictures.Add(newPicture); cnt++; } //save changes ctx.SaveChanges(); } } else { //some error happened, retry TempData["error"] = abuResponse.responseWrapper.message; return(RedirectToAction("AddNewBookingUnit", "Agent")); } TempData["success"] = "Successfully added a new booking unit"; return(RedirectToAction("AgentPage", "Agent")); } else { //image type exception TempData["error"] = "Wrong image type, please try again"; return(RedirectToAction("AddNewBookingUnit", "Agent")); } } else { //invalid VM exception TempData["error"] = "Some form atributes are incorrect"; return(RedirectToAction("AddNewBookingUnit", "Agent")); } } }