示例#1
0
        public ActionResult Create(BlogPost blogpost)
        {
            // Don't let non logged in users make posts
            if (!User.Identity.IsAuthenticated)
            {
                ModelState.AddModelError(string.Empty,
                    "You must login to create a post!");
            }
            else if (ModelState.IsValid)
            {
                // Set Creation date
                blogpost.DateCreated = DateTime.Now;

                // Set current user as the author
                blogpost.Author = User.Identity.Name;

                db.BlogPosts.Add(blogpost);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(blogpost);
        }
示例#2
0
 public ActionResult Edit(BlogPost editedpost)
 {
     // Retrieve actual post from DB that holds more data
     BlogPost post = db.BlogPosts.Find(editedpost.ID);
     // validate user
     if (User.Identity.Name != post.Author)
     {
         ModelState.AddModelError(string.Empty,
             "unable to edit blog post as you are not the author!");
     }
     else if (ModelState.IsValid)
     {
         // Fill edited data into the post from the DB
         post.Title = editedpost.Title;
         post.Post = editedpost.Post;
         db.Entry(post).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(post);
 }