public async Task <IActionResult> Create([Bind("Id,Title,ImagePath,Description,File")] GemstoneCreateViewModel viewModel, IFormFile image) { ModelState.Remove("User"); ModelState.Remove("UserId"); var user = await GetCurrentUserAsync(); if (ModelState.IsValid) { var gemstone = new Gemstone() { Title = viewModel.Title, Description = viewModel.Description, }; if (viewModel.File != null && viewModel.File.Length > 0) { var fileName = Path.GetFileName(viewModel.File.FileName); //getting path of actual file name var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\images", fileName); //creating path combining file name w/ www.root\\images directory using (var fileSteam = new FileStream(filePath, FileMode.Create)) //using filestream to get the actual path { await viewModel.File.CopyToAsync(fileSteam); } gemstone.ImagePath = fileName; } gemstone.UserId = user.Id; _context.Add(gemstone); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["UserId"] = new SelectList(_context.ApplicationUsers, "Id", "Id", viewModel.UserId); return(View(viewModel)); }
// GET: Gemstones/Create public async Task <IActionResult> CreateAsync() { var viewModel = new GemstoneCreateViewModel(); var user = await GetCurrentUserAsync(); ViewData["UserId"] = new SelectList(_context.ApplicationUsers, "Id", "Id"); return(View(viewModel)); }