public async Task <IActionResult> Create([Bind("UserFishId,UserAccountId,FishName,FishLength,UserFishPhotoPath,CatchDate")] Userfish userfish, IFormFile FilePhoto) { if (FilePhoto.Length > 0) { string photoPath = _webroot.WebRootPath + "\\userPhoto\\"; var filename = Path.GetFileName(FilePhoto.FileName); using (var stream = System.IO.File.Create(photoPath + filename)) { await FilePhoto.CopyToAsync(stream); userfish.UserFishPhotoPath = filename; } } if (ModelState.IsValid) { _context.Add(userfish); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["FishName"] = new SelectList(_context.FishInfo, "FishName", "FishName", userfish.FishName); // ViewData["UserAccountId"] = new SelectList(_context.UserProfile, "UserAccountId", "FirstName", userfish.UserAccountId); return(View(userfish)); }
public async Task <IActionResult> Edit(int id, [Bind("UserFishId,UserAccountId,FishName,FishLength,UserFishPhotoPath,CatchDate")] Userfish userfish, IFormFile FilePhoto) { if (FilePhoto.Length > 0) { string photoPath = _webroot.WebRootPath + "\\userPhoto\\"; var filename = Path.GetFileName(FilePhoto.FileName); using (var stream = System.IO.File.Create(photoPath + filename)) { await FilePhoto.CopyToAsync(stream); userfish.UserFishPhotoPath = filename; } } if (id != userfish.UserFishId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(userfish); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserfishExists(userfish.UserFishId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["FishName"] = new SelectList(_context.FishInfo, "FishName", "FishName", userfish.FishName); //ViewData["UserAccountId"] = new SelectList(_context.UserProfile, "UserAccountId", "FirstName", userfish.UserAccountId); return(View(userfish)); }