Exemple #1
0
        public ActionResult NewPost(Post post, Picture picture)
        {
            var            user     = (User)Session["user"];
            PostRepository postRepo = new PostRepository();

            if (ModelState.IsValid)
            {
                if (picture.files.Count(file => file != null) > 0)
                {
                    post.PostDateTime = DateTime.Now;
                    post.UserId       = user.Id;
                    post.Status       = "Pending";
                    post.Views        = 0;
                    postRepo.Insert(post);

                    foreach (var file in picture.files)
                    {
                        if (file.ContentLength > 0)
                        {
                            string fileName  = Path.GetFileNameWithoutExtension(file.FileName);
                            string extension = Path.GetExtension(file.FileName);
                            fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
                            var path = Path.Combine(Server.MapPath("~/Content/PostImages"), fileName);
                            file.SaveAs(path);
                            PostImageRepository postImgRepo = new PostImageRepository();
                            PostImage           pimg        = new PostImage();
                            pimg.ImgPath = fileName;
                            pimg.PostId  = post.Id;
                            postImgRepo.Insert(pimg);
                        }
                        else
                        {
                            TempData["msg"] = "Invalid Image!!";
                            return(RedirectToAction("NewPost"));
                        }
                    }
                    TempData["msg"] = "New Post Added!!";
                    return(RedirectToAction("NewPost"));
                }
                else
                {
                    TempData["msg"] = "Invalid Image!!";
                    return(RedirectToAction("NewPost"));
                }
            }
            else
            {
                ModelState.Values.SelectMany(v => v.Errors).ElementAt(0);

                return(View());
            }
        }