コード例 #1
0
 public ActionResult Add(Blog blog, int?ID, HttpPostedFileBase ImageURL)
 {
     if (string.IsNullOrWhiteSpace(blog.BlogTitle) | string.IsNullOrWhiteSpace(blog.BlogDescription) | (blog.BlogType == 0 || blog.BlogType == null))
     {
         return(View());
     }
     ViewBag.BlogTypeList = entity.BlogTypes.ToList();
     if (ID != null)
     {
         Blog oldBlog = entity.Blogs.FirstOrDefault(x => x.ID == ID);
         oldBlog.BlogTitle       = blog.BlogTitle;
         oldBlog.BlogDescription = blog.BlogDescription;
         if (blog.ImageURL != null)
         {
             oldBlog.ImageURL = Path.GetFileName(ImageURL.FileName);
             string path = Path.Combine(Server.MapPath("~/upload"), Path.GetFileName(ImageURL.FileName));
             ImageURL.SaveAs(path);
         }
         oldBlog.BlogType   = blog.BlogType;
         oldBlog.BlogWriter = blog.BlogWriter;
         blog.Datetime      = DateTime.Now;
     }
     else
     {
         if (blog.ImageURL != null)
         {
             string path = Path.Combine(Server.MapPath("~/upload"), Path.GetFileName(ImageURL.FileName));
             ImageURL.SaveAs(path);
         }
         blog.Datetime = DateTime.Now;
         entity.Blogs.Add(blog);
     }
     entity.SaveChanges();
     return(Redirect("/ControlPanel/Index"));
 }
コード例 #2
0
        public ActionResult CommentAdd(Comment comment, int?ID)
        {
            if (ID != null)
            {
                comment.DateTime = DateTime.Now;
                comment.BlogID   = ID;
                entity.Comments.Add(comment);
                entity.SaveChanges();

                return(Redirect("/Blog/Detail/" + ID));
            }
            else
            {
                return(View());
            }
        }