public ActionResult UploadFiles(int automobileId, HttpPostedFileBase[] files) { bool success = false; //Ensure model state is valid if (ModelState.IsValid) { //iterating through multiple file collection foreach (HttpPostedFileBase file in files) { //Checking file is available to save. if (file != null && automobileId != 0) { //variables used by returning of message for pass/fail upload int counter = files.Count(); if (Path.GetExtension(file.FileName).ToLower() == ".jpg" || Path.GetExtension(file.FileName).ToLower() == ".png" || Path.GetExtension(file.FileName).ToLower() == ".gif" || Path.GetExtension(file.FileName).ToLower() == ".jpeg") { var InputFileName = Path.GetFileName(file.FileName); var ServerSavePath = Path.Combine(Server.MapPath("~/Content/Images/") + InputFileName); //Save file to server folder file.SaveAs(ServerSavePath); //add the images (their path and automobileId relation) to the DB var image = new FileModel() { ImagePath = "~/Content/Images/" + InputFileName, AutomobileId = automobileId }; _context.FileModels.Add(image); } else { counter--; } //assigning file uploaded status to ViewBag for showing message to user. if (counter != files.Count()) { ViewBag.UploadStatus = "There are files which are not images." + System.Environment.NewLine + counter + " files uploaded successfully."; } else { success = true; ViewBag.UploadStatus = counter + " files uploaded successfully."; } } } _context.SaveChanges(); } //return to the UploadFiles with information for the pass/fail upload IEnumerable <FileModel> filesInDb = _context.FileModels.ToList().Where(c => c.AutomobileId == automobileId); var imageModel = new ImagesFormViewModel() { AutomobileId = automobileId, FileModels = filesInDb, Success = success }; return(View(imageModel)); }
public ActionResult Save(Automobile automobile, List <AutomobileDetail> automobileDetails) { if (!ModelState.IsValid) { var viewModel = new AutomobileFormViewModel(automobile) { Countries = _context.Countries.ToList(), AutomobileMakes = _context.AutomobileMakes.ToList(), AutomobileModels = _context.AutomobileModels.ToList(), Details = _context.Details.ToList(), Engines = _context.Engines.ToList(), AutomobileDetails = automobileDetails }; return(View("AutomobileForm", viewModel)); } // create a variable 'automobileId' which will be passed as parameter to the 'UploadFiles' view int automobileId; // check if we create new automobile or edit existing one if (automobile.Id == 0) { automobile.DateImported = DateTime.Now; automobile.StatusId = 1; _context.Automobiles.Add(automobile); _context.SaveChanges(); List <Detail> details = _context.Details.ToList(); // List<Automobile> automobiles = _context.Automobiles.ToList(); var automobileLastId = _context.Automobiles.ToList().Last().Id; // var automobileLastId1 = _context.Automobiles.Max() // automobileId = automobiles.Last().Id; automobileId = automobileLastId; if (details.Any()) { for (int i = 0; i < details.Count(); i++) { automobileDetails[i].DetailId = details[i].Id; // automobileDetails[i].AutomobileId = automobiles.Last().Id; automobileDetails[i].AutomobileId = automobileLastId; _context.AutomobileDetails.Add(automobileDetails[i]); } } } else { automobileId = automobile.Id; var automobileInDb = _context.Automobiles.Single(c => c.Id == automobile.Id); automobileInDb.ReleaseDate = automobile.ReleaseDate; automobileInDb.CountryId = automobile.CountryId; automobileInDb.AutomobileMakeId = automobile.AutomobileMakeId; automobileInDb.AutomobileModelId = automobile.AutomobileModelId; automobileInDb.EngineId = automobile.EngineId; automobileInDb.Color = automobile.Color; automobileInDb.Transmission = automobile.Transmission; automobileInDb.Miles = automobile.Miles; if (automobileDetails.Any()) { //we take all automobileDetails correcponding to the current automobile, then set their Vaues //to the new Values coming from the view in the list automobileDetails IEnumerable <AutomobileDetail> automobileDetailsInDb = _context.AutomobileDetails.Where(c => c.AutomobileId == automobile.Id); List <AutomobileDetail> automobileDetailsOld = automobileDetails.FindAll(c => c.AutomobileId != null); int n = 0; foreach (var automobileDetail in automobileDetailsInDb) { automobileDetail.DetailValue = automobileDetailsOld[n].DetailValue; n++; } if (automobileDetails.Any(c => c.AutomobileId == null)) { //there are new automobileDetails wich should be created for first time //we set their AutomobileId to the Id of the returned from the view automobile List <AutomobileDetail> automobileDetailsNew = automobileDetails.FindAll(c => c.AutomobileId == null); List <Detail> detailsInDb = _context.Details.ToList(); foreach (var automobileDetail in automobileDetailsNew) { automobileDetail.AutomobileId = automobile.Id; } // we find the diference between all details and these which should be added in automobileDetails table // then in a loop set their detailsId and finally Add() them to the table in the DB // int dif = detailsInDb.Count() - automobileDetailsNew.Count(); int automobileDetailsNewCount = automobileDetailsNew.Count(); int detailCount = detailsInDb.Count(); for (int i = 0; i < automobileDetailsNewCount; i++) { automobileDetailsNew[i].DetailId = detailsInDb[detailCount - automobileDetailsNewCount + i].Id; _context.AutomobileDetails.Add(automobileDetailsNew[i]); } } } } _context.SaveChanges(); //the next lines prepare image model which will be used to call the Uploadfiles view IEnumerable <FileModel> filesInDb = _context.FileModels.ToList().Where(c => c.AutomobileId == automobileId); var imageModel = new ImagesFormViewModel() { AutomobileId = automobileId, FileModels = filesInDb }; // return RedirectToAction("Index", "Automobiles"); // return RedirectToAction("UploadFiles", "FileModels", automobileId); return(View("~/Views/FileModels/UploadFiles.cshtml", imageModel)); }
public ActionResult UploadFiles(ImagesFormViewModel imageModel) { return(View(imageModel)); }