public async Task <IActionResult> Edit(int id, anahaber anahaber) { if (id != anahaber.Id) { return(NotFound()); } if (ModelState.IsValid) { try { voiduploadsphoto(anahaber); _context.Update(anahaber); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!anahaberExists(anahaber.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(anahaber)); }
public async Task <IActionResult> Create(anahaber anahaber) { if (ModelState.IsValid) { voiduploadsphoto(anahaber); _context.Add(anahaber); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(anahaber)); }
void voiduploadsphoto(anahaber haber) { if (haber.File != null) { string uploadfile = Path.Combine(host.WebRootPath, "img/news"); string uniname = Guid.NewGuid() + ".jpg"; string filepath = Path.Combine(uploadfile, uniname); using (var fileStream = new FileStream(filepath, FileMode.Create)) { haber.File.CopyTo(fileStream); } haber.image = uniname; } }