public async Task <IActionResult> Create(CreatePlantAuditViewModel model, int id, IFormFile file) { ModelState.Remove("User"); ModelState.Remove("UserId"); ModelState.Remove("PlantAudit.PlantId"); ModelState.Remove("PlantAudit.UpdatedImage"); // FILE UPLOAD // make sure file is selected if (file == null || file.Length == 0) { return(Content("file not selected")); } // get path location to store img string path_Root = _appEnvironment.WebRootPath; // get only file name without file path var trimmedFileName = System.Guid.NewGuid().ToString() + System.IO.Path.GetFileName(file.FileName); // store file location string path_to_Images = path_Root + "\\User_Files\\Images\\" + trimmedFileName; if (ModelState.IsValid) { // Get the current user var user = await GetCurrentUserAsync(); // get current plant by id var plant = await _context.Plant.FindAsync(id); // match plant audit with current plant model.PlantAudit.PlantId = plant.PlantId; // assign plant water id to selected water id plant.WaterId = model.PlantAudit.WaterId; // assign plant light id to selected light id plant.LightId = model.PlantAudit.LightId; model.Plant = plant; // copy file to target using (var stream = new FileStream(path_to_Images, FileMode.Create)) { await file.CopyToAsync(stream); } model.PlantAudit.UpdatedImage = trimmedFileName; _context.Add(model.PlantAudit); await _context.SaveChangesAsync(); ViewData["FilePath"] = path_to_Images; return(RedirectToAction("Details", "Plants", new { @id = id })); } return(View(model)); }
// GET: PlantAudits/Create/4 public async Task <IActionResult> Create(int?id) { if (id == null) { return(NotFound()); } // get plant based on id passed in var plant = await _context.Plant.FindAsync(id); CreatePlantAuditViewModel viewModel = new CreatePlantAuditViewModel(_context); viewModel.Plant = plant; return(View(viewModel)); }