예제 #1
0
 public ActionResult Create(BlogPost Post)
 {
     Post.Created = System.DateTimeOffset.Now;
     db.Posts.Add(Post);
     db.SaveChanges();
     return RedirectToAction("Index");
 }
예제 #2
0
 public ActionResult Edit(BlogPost Post)
 {
     Post.Updated = System.DateTimeOffset.Now;
     db.Posts.Attach(Post);
     db.Entry(Post).Property("Title").IsModified = true;
     db.Entry(Post).Property("Body").IsModified = true;
     db.SaveChanges();
     return RedirectToAction("Index");
 }
예제 #3
0
        public ActionResult Edit(BlogPost blogPost, HttpPostedFileBase image)
        {
            if (blogPost == null)
            {
                return HttpNotFound();
            }
            if (image != null && image.ContentLength > 0)
            {
                var ext = Path.GetExtension(image.FileName).ToLower();
                if (ext != ".png" && ext != ".jpg" && ext != ".jpeg" && ext != ".gif" && ext != ".bmp")
                {
                    ModelState.AddModelError("image", "Invalid Format");
                }
            }

            if (ModelState.IsValid)
            {
                if(image !=null)
                {
                    var filePath = "/Uploads/";
                    var absPath = Server.MapPath("~" + filePath);
                    blogPost.MediaURL = filePath + image.FileName;
                    Directory.CreateDirectory(absPath);
                    image.SaveAs(Path.Combine(absPath, image.FileName));
                }
                blogPost.Updated = System.DateTimeOffset.Now;
                db.Posts.Attach(blogPost);
                db.Entry(blogPost).Property("Body").IsModified = true;
                db.Entry(blogPost).Property("Updated").IsModified = true;
                db.Entry(blogPost).Property("MediaURL").IsModified = true;
                db.Entry(blogPost).Property("Published").IsModified = true;
                //db.Entry(blogPost).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(blogPost);
        }