public ActionResult ProjectDelete(ProjectViewModel projectViewModel) { using (CahniteContext db = new CahniteContext()) { Project project = db.Projects.SingleOrDefault(p => p.ID == projectViewModel.ID); if (project != null) { db.Projects.Remove(project); db.SaveChanges(); return(RedirectToAction("Index")); } return(new HttpNotFoundResult()); } }
public ActionResult ProjectCreateEdit(ProjectViewModel projectViewModel) { using (CahniteContext db = new CahniteContext()) { Project project; // makes a new project if creating if (projectViewModel.ID <= 0) { project = new Project { CreatedOn = DateTime.Now, EditedOn = DateTime.Now }; } // otherwise assign an existing project to var project else { project = db.Projects.Single(p => p.ID == projectViewModel.ID); } if (project != null) { project.Title = projectViewModel.Title; project.Intro = projectViewModel.Intro; project.BodyHtml = projectViewModel.BodyHtml; project.ImageUrl = projectViewModel.ImageUrl; project.Favorite = projectViewModel.Favorite; project.Publish = projectViewModel.Publish; project.EditedOn = DateTime.Now; if (projectViewModel.RePublish) { project.CreatedOn = DateTime.Now; } if (ModelState.IsValid) { // auto-defines the url parameter used to access the detail view project.Url = projectViewModel.Title.Replace(" ", "_"); if (project.ID != 0) { db.SaveChanges(); } else { db.Projects.Add(project); db.SaveChanges(); } return(RedirectToAction("Index")); } // these need defining before returing the ViewModel, otherwise the wrong dates are shown // if there are validation errors and the ViewModel is returned to the edit view w/ warnings projectViewModel.CreatedOn = project.CreatedOn; projectViewModel.EditedOn = project.EditedOn; return(View(projectViewModel)); } } return(new HttpNotFoundResult()); }