public ActionResult Create(LicentaViewModel model) { // Check model state if (!ModelState.IsValid) { return(View(model)); } using (ApplicationDbContext db = new ApplicationDbContext()) { LicentaDTO dto = new LicentaDTO(); // DtO Title dto.Title = model.Title; //Make sure title and slug are unique if (db.Licenta.Any(x => x.Title == model.Title)) { ModelState.AddModelError("", "That title already exists."); return(View(model)); } //DTO the rest dto.Content = model.Content; dto.UserId = User.Identity.GetUserId(); dto.Created = DateTime.Now; //Save DTO db.Licenta.Add(dto); db.SaveChanges(); } //Set TempData message TempData["SMM"] = "You have added a new event!"; //Redirect return(RedirectToAction("Index")); }
public ActionResult Edit(LicentaViewModel model) { // Check model state if (!ModelState.IsValid) { return(View(model)); } using (ApplicationDbContext db = new ApplicationDbContext()) { // Get page id int id = model.Id; // Get the page LicentaDTO dto = db.Licenta.Find(id); // DTO the title dto.Title = model.Title; // Make sure title and slug are unique if (db.Licenta.Where(x => x.Id != id).Any(x => x.Title == model.Title)) { ModelState.AddModelError("", "That title already exists."); return(View(model)); } // DTO the rest dto.Content = model.Content; dto.Edited = DateTime.Now; // Save the DTO db.SaveChanges(); } // Set TempData message TempData["SMM"] = "You have edited the post!"; // Redirect return(RedirectToAction("Index")); }
public ActionResult Edit(int id) { // Declare pageVM LicentaViewModel model; using (ApplicationDbContext db = new ApplicationDbContext()) { // Get the page LicentaDTO dto = db.Licenta.Find(id); // Confirm page exists if (dto == null) { return(Content("The post does not exist.")); } // Init pageVM model = new LicentaViewModel(dto); } // Return view with model return(View(model)); }