public async System.Threading.Tasks.Task <ActionResult> MakeADonation(DonationViewModels model) { //Setting donation details from formData //TODO: //Save to database if (!ModelState.IsValid) { return(View(model)); } List <HttpPostedFile> files = new List <HttpPostedFile>(); foreach (var f in Request.Files) { files.Add(f as HttpPostedFile); } try { var HeaderImagesJson = Request.Params["headerImages"]; var donationDetailsJson = Request.Params["details"]; //Deserialization var donationImages = JsonConvert.DeserializeObject <List <FileViewModel> >(HeaderImagesJson); var donationDetails = JsonConvert.DeserializeObject <List <DonationDetailsViewModels> >(donationDetailsJson); List <string> filesAdded = new List <string>(); model.DonationDetails = donationDetails; var imageService = new ImageService(log); ViewBag.StatusMessage = ""; //Amount validation if money is on if (model.IsMoney && model.Amount <= 0) { ViewBag.StatusMessage = Resources.InvalidAmount; return(View(model)); } filesAdded = new List <string>(); foreach (var line in model.DonationDetails) { foreach (var file in line.MultiFileData) { //check if the image already exist. var fileExist = filesAdded.Any(x => x == file.Name); if (!fileExist) { //upload image to azure var fileToUpload = file.GetFile(Request.Files); if (fileToUpload != null) { var imageUrl = await imageService.CreateUploadedImageAsync(fileToUpload, Guid.NewGuid().ToString(), true, 500, 500); line.Images.Add(new ImageViewModel() { Item = line, ImageUrl = imageUrl }); filesAdded.Add(file.Name); } } } } //Class Injection CurrentUser = userRepository.GetByUserName(User.Identity.Name); var donation = new Donation { Amount = model.Amount, CreatedBy = CurrentUser, CreatedOn = DateTime.Now, DonatedOn = DateTime.Now, Description = model.Description, DonationStatus = (Core.Entities.DonationStatus)model.DonationStatus, ExpireOn = model.ExpireOn, Title = model.Title, ModifiedOn = null }; //Donation Images filesAdded = new List <string>(); foreach (var fileWrapper in model.HeaderMultiFileData) { //check if the image already exist. var fileExist = filesAdded.Any(x => x == fileWrapper.FileName); if (!fileExist) { var file = fileWrapper as HttpPostedFileBase; var imageUrl = await imageService.CreateUploadedImageAsync(file, Guid.NewGuid().ToString(), true, 500, 500); donation.AddImage(new DonationImage() { Donation = donation, ImageUrl = imageUrl, CreatedBy = CurrentUser, CreatedOn = DateTime.Now, ModifiedOn = null }); filesAdded.Add(fileWrapper.FileName); } } if (model.DonationDetails != null && model.DonationDetails.Count() > 0) { donation.DonationDetails = new List <DonationDetails>(); } else { donation.DonationDetails = null; } if (!model.IsMoney) { foreach (var item in model.DonationDetails) { var itemLine = new DonationDetails { CreatedBy = CurrentUser, Donation = donation, CreatedOn = DateTime.Now, ItemName = item.ItemName, ItemType = (Core.Entities.DonationType)item.ItemType, Line = item.Index, DonationStatus = (Core.Entities.DonationStatus)item.DonationStatus, ModifiedOn = null }; foreach (var image in item.Images) { itemLine.Images.Add( new DonationDetailsImage { CreatedBy = CurrentUser, Item = itemLine, ImageUrl = image.ImageUrl, CreatedOn = DateTime.Now, ModifiedOn = null }); } donation.DonationDetails.Add(itemLine); } } //save to database donationRepository.Create(donation); //Success return(Json(new { result = "Redirect", url = Url.Action("Index", "Donations", new { Message = DonationMessageId.CrateDonationSuccess }) })); } catch (System.Exception ex) { ViewBag.StatusMessage = ex.Message; } return(Json(new { result = "Error", Message = ViewBag.StatusMessage }));; }