Пример #1
0
        public ActionResult Edit(Post post)
        {
            if(ModelState.IsValid)
            {
                post.CreationDate = DateTime.Now;
                int idFileName=postsRepository.SavePost(post);

                foreach (string file in Request.Files)
                {
                    var uploadedFile = this.Request.Files[file];
                    if (uploadedFile != null && uploadedFile.ContentLength > 0)
                    {
                        if (uploadedFile.ContentLength > 1024*1024*4)
                        {
                            ModelState.AddModelError("uploadError", "File size can't exceed 4 MB");
                            return View(post);
                        }

                        string fileName = uploadedFile.FileName;
                        string[] splitFileName = fileName.Split('.');
                        fileName =
                            Path.GetFileName(idFileName.ToString() + '.' + splitFileName[splitFileName.Length - 1]);

                        var path = Path.Combine(Server.MapPath("~/Content/PostImages/"), fileName);

                        try
                        {
                            if (System.IO.File.Exists(path))
                                System.IO.File.Delete(path);

                            uploadedFile.SaveAs(path);
                        }
                        catch (Exception)
                        {

                            ModelState.AddModelError("uploadError", "Can't save file to disk");
                        }

                    }

                }

                TempData["message"] = string.Format("{0} has been saved", post.Name);
                TempData["selectedIsuue"] = "PostsAdmin";
                return RedirectToAction("ShowPosts");
            }
            else
            {
                // there is something wrong with the data values
                return View(post);
            }
        }
Пример #2
0
        public int SavePost(Post post)
        {
            int result = 0;
            if(post.PostID==0)
            {
                context.Posts.Add(post);
                HttpRuntime.Cache.Remove("Posts");
            }
            else
            {
                context.Entry(post).State=EntityState.Modified;
                HttpRuntime.Cache.Remove("Posts");
                HttpRuntime.Cache.Remove("Post" + post.PostID);
            }

            context.SaveChanges();
            result = post.PostID;
            return result;
        }
Пример #3
0
 public void DeletePost(Post post)
 {
     context.Posts.Remove(post);
     context.SaveChanges();
     HttpRuntime.Cache.Remove("Posts");
 }