public async Task <IActionResult> Create([Bind("Id,Name,AddedOn,AddedBy")] Tag tag) { if (ModelState.IsValid) { _context.Add(tag); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(tag)); }
public async Task <IActionResult> Create(DocumentViewModel documentVM, IFormFile file) { var document = new Document { }; if (ModelState.IsValid) { var fileSize = file.Length; if (fileSize > 0) { var folder = Path.Combine(webHostEnvironment.WebRootPath, "files"); var uniqueFileName = Guid.NewGuid().ToString() + "_" + file.FileName; var filePath = Path.Combine(folder, uniqueFileName); using var stream = new FileStream(filePath, FileMode.Create); await file.CopyToAsync(stream); document = new Document { Name = file.FileName, FileSize = file.Length, Tag = documentVM.Tag, AddedBy = "mike", // temporary placeholder until actual login is added. AddedOn = DateTime.Now, LastEdited = DateTime.Now, FilePath = uniqueFileName }; } _context.Add(document); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(document)); }