public ActionResult Create(BlogInputModel model) { //Executes the following only when the data inputted is //of the correct format if(model != null && this.ModelState.IsValid) { //Finds the current user using the system var currentUser = User.Identity.GetUserId(); //Creates a new post based on the model var newBlog = new Post() { PostId = model.Id, UserId = currentUser, Title = model.Title, Category = model.Category, Content = model.Content, Author = db.Users.OfType<Admin>().FirstOrDefault(e=> e.Id.Equals(currentUser)) }; //Adds new post to the database this.db.Posts.Add(newBlog); this.db.SaveChanges(); this.AddNotification("Post Created Successfully", NotificationType.SUCCESS); //Redirects to the list of blogs return this.RedirectToAction("BlogList"); } this.AddNotification("Oops, a post wasn't created", NotificationType.ERROR); //Returns to the post page return this.View(model); }
public ActionResult Edit(int Id, BlogInputModel model) { //Finds blog that has the primary key of the value of 'id' var editBlog = this.FindBlog(Id); //Redirects back to the blog list if no blog is found if (editBlog == null) { return this.RedirectToAction("BlogList"); } //Updates the data inside database //Executes this only if the data entered is correct if (model != null && this.ModelState.IsValid) { editBlog.Title = model.Title; editBlog.Content = model.Content; editBlog.Category = model.Category; //Updates the values of the entry and redirects back to the blog list this.db.SaveChanges(); this.AddNotification("Post Updated Successfully", NotificationType.SUCCESS); return this.RedirectToAction("BlogList"); } //Returnds to the edit page with the post data this.AddNotification("Ooops, Post Update Failed", NotificationType.ERROR); return this.View(model); }