public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } Place = await _context.Place.FindAsync(id); if (Place != null) { Place.Active = "NO"; await _context.SaveChangesAsync(); } return(RedirectToPage("Index", new { userId = Place.UserId })); }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } string img = ImgFromDb(Place); _context.Attach(Place).State = EntityState.Modified; string webRootPath = _hostingEnvironment.WebRootPath; var files = HttpContext.Request.Form.Files; var PlaceFromdb = _context.Place.Find(Place.Id); if (files.Count == 0) { Place.Image = img; } else { var uploads = Path.Combine(webRootPath, "images"); var extension = files[0].FileName.Substring(files[0].FileName.LastIndexOf("."), files[0].FileName.Length - files[0].FileName.LastIndexOf(".")); if (System.IO.File.Exists(Path.Combine(uploads, Place.Id + extension))) { System.IO.File.Delete(Path.Combine(uploads, Place.Id + extension)); } extension = files[0].FileName.Substring(files[0].FileName.LastIndexOf("."), files[0].FileName.Length - files[0].FileName.LastIndexOf(".")); using (var fileStream = new FileStream(Path.Combine(uploads, Place.Id + extension), FileMode.Create)) { files[0].CopyTo(fileStream); } Place.Image = @"\images\" + Place.Id + extension; } PlaceFromdb.Active = "YES"; await _context.SaveChangesAsync(); return(RedirectToPage("Index", new { userId = PlaceFromdb.UserId })); }