public async Task <IActionResult> Edit(int id, [Bind("Id,Path,Description,EventId")] File file) { if (id != file.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(file); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!FileExists(file.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(file)); }
public async Task <IActionResult> Create(FileViewModel fileViewModel) { if (ModelState.IsValid) { var file = new File(); if (fileViewModel.MyFile != null) { var uniqueFileName = GetUniqueFileName(fileViewModel.MyFile.FileName); var uploads = Path.Combine(hostingEnvironment.WebRootPath, "uploads"); var filePath = Path.Combine(uploads, uniqueFileName); await fileViewModel.MyFile.CopyToAsync(new FileStream(filePath, FileMode.Create)); //to do : Save uniqueFileName to your db table file.UniqueFileName = uniqueFileName; file.Path = filePath; file.Description = fileViewModel.Description; file.EventId = fileViewModel.EventId; } _context.Add(file); await _context.SaveChangesAsync(); // agregamos la referencia del file al evento al que pertenece var @event = await _context.Events.FirstOrDefaultAsync(m => m.Id == file.EventId); @event.FileId = file.Id; await _context.SaveChangesAsync(); if (@event is PracticalSession) { return(RedirectToAction("Details", "PracticalSession", new { id = file.EventId.ToString() })); } else { return(RedirectToAction("Details", "Talk", new { id = file.EventId.ToString() })); } } return(View(fileViewModel)); }