public async Task <IActionResult> Create(Models.Event.Event newevent) { if (ModelState.IsValid) { if (newevent.ImageFile != null) { string wwwRootPath = hostEnvironment.WebRootPath; string fileName = Path.GetFileNameWithoutExtension(newevent.ImageFile.FileName); string extension = Path.GetExtension(newevent.ImageFile.FileName); fileName = fileName + DateTime.Now.ToString("yymmss") + extension; newevent.ImageName = fileName; string path = Path.Combine(wwwRootPath + "/Image/", fileName); using (var fileStream = new FileStream(path, FileMode.Create)) { await newevent.ImageFile.CopyToAsync(fileStream); } } var user = await userManager.GetUserAsync(User); newevent.UserID = user.Id; newevent.UserName = User.Identity.Name; repository.SaveEvent(newevent); TempData["message"] = $"{newevent.Header} has been saved"; return(RedirectToAction("Index", "EAdmin")); } else { // there is something wrong with the data values return(View("~/Views/Event/Create.cshtml", newevent)); } }
public IActionResult Delete(int eventId) { Models.Event.Event temp = repository.Events.FirstOrDefault(p => p.EventID == eventId); if (temp.ImageName != null) { var imagePath = Path.Combine(hostEnvironment.WebRootPath, "image", temp.ImageName); if (System.IO.File.Exists(imagePath)) { System.IO.File.Delete(imagePath); } } Models.Event.Event deletedEvent = repository.DeleteEvent(eventId); if (deletedEvent != null) { TempData["message"] = $"{deletedEvent.Header} was deleted"; } return(RedirectToAction("Index", "EAdmin")); }