public ActionResult Create(PostInputModel inputModel) { // first check if slug exists if (Posts.Exists(x => x.Slug == inputModel.Slug && x.IsActive)) ModelState.AddModelError("Slug", "Post slug must be unique."); if (ModelState.IsValid) { var post = new Post(); Mapper.Map(inputModel, post); post.CreatedOn = DateTime.Now; if (post.IsActive) post.PublishedOn = DateTime.Now; post = Posts.Add(post); this.FlashInfo("Published \"{0}\" on {1}".Fmt(post.Title, post.PublishedOn)); return RedirectToAction("Display", new { slug = post.Slug, area = "" }); } return View(inputModel); }
public ActionResult Edit(string id, PostInputModel inputModel) { var post = Posts.GetById(id); if (post == null) return HttpNotFound("no such post"); // check if slug exists if (Posts.Exists(x => x.Slug == inputModel.Slug && x.IsActive && x.Id != id)) ModelState.AddModelError("Slug", "Post slug must be unique."); if (ModelState.IsValid) { Mapper.Map(inputModel, post); post.UpdatedOn = DateTime.Now; post.Format = PostFormat.Markdown; // this should be coming from the input model Posts.Update(post); this.FlashInfo("Updated page."); return RedirectToAction("Display", new { controller = "Post", area = string.Empty, slug = post.Slug }); } return View(inputModel); }