public ContentResult AddPost(Post post) { string json; //variable for passing successful or unsuccessful json data ModelState.Clear(); //remove items from the model state dictionary // if the current post is valid if (TryValidateModel(post)) { post.PostedOn = DateTime.Now; // assign the new post a posted on date based on current time var id = blogRepository.AddPost(post); // get id from newly added post and attempt to save data // if id equals the new id, post was successful json = JsonConvert.SerializeObject(new { id = id, success = true, message = "Post added succesfully." }); } //else if id is 0, post was not successful else { json = JsonConvert.SerializeObject(new { id = 0, success = false, message = "Failed to add the post." }); } return Content(json, "application/json"); //return the json data to show user if post was successful or not }
// returns the Id of the added post public int AddPost(Post post) { context.Posts.Add(post); //add the post to queue if (post.Tags != null) { foreach (Tag tag in post.Tags) { PostTagMap postTag = new PostTagMap(); postTag.Post_id = post.PostId; postTag.Tag_id = tag.TagId; context.PostTagMaps.Add(postTag); context.SaveChanges(); } } context.SaveChanges(); // save changes in queue return post.PostId; }
//html helper method that takes a Post object and turns it into a link with a css id using the posts title public static MvcHtmlString PostLink(this HtmlHelper helper, Post post) { return helper.ActionLink(post.Title, "Post", "Blog", new { title = post.UrlSlug }, new { title = post.Title, id = "post-link" }); }
// method that saves the edited changes in a post public void EditPost(Post post) { post.Modified = DateTime.Now; // updates modified datetime context.Posts.Attach(post); // adds post to queue. context.Entry(post).State = EntityState.Modified; //makes post's state set to modified context.SaveChanges(); // saves changes in queue }
public ContentResult EditPost(Post post) { string json; //variable for passing successful or unsuccessful json data ModelState.Clear(); //remove items from the model state dictionary // if the current post is valid if (TryValidateModel(post)) { blogRepository.EditPost(post); // get id from newly edited post and attempt to save data // if id = edited post id, change was successful json = JsonConvert.SerializeObject(new { id = post.PostId, success = true, message = "Changes saved successfully." }); } //else if id = 0, changes failed. else { json = JsonConvert.SerializeObject(new { id = 0, success = false, message = "Failed to save the changes." }); } return Content(json, "application/json"); //return the json data to show user if editing the post was successful or not }