public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            BlogPost post = db.BlogPosts.Find(id);

            if (post == null)
            {
                return(HttpNotFound());
            }

            BlogTopicsViewModel model = new BlogTopicsViewModel();

            model.Post = post;
            string[] selectedItemIds = new string[post.Topics.Count];
            int      i = 0;

            foreach (var topic in post.Topics)
            {
                selectedItemIds[i] = topic.Id.ToString();
                i++;
            }


            ViewBag.Topics = new MultiSelectList(db.Topics, "Id", "Description", selectedItemIds);
            return(View(model));
        }
Ejemplo n.º 2
0
        public ActionResult Create([Bind(Include = "Post, Topics")] BlogTopicsViewModel model, HttpPostedFileBase Image, string button)
        {
            if (ModelState.IsValid)
            {
                var Slug = StringUtilities.URLFriendly(model.Post.Title);
                if (String.IsNullOrWhiteSpace(Slug))
                {
                    ModelState.AddModelError("Title", "Invalid title");
                    return(View(model));
                }
                if (db.Posts.Any(p => p.Slug == Slug))
                {
                    ModelState.AddModelError("Title", "The title must be unique");
                    return(View(model));
                }
                var ErrorMessage = "";
                if (ImageUploadValidator.IsWebFriendlyImage(Image))
                {
                    var fileName   = Path.GetFileName(Image.FileName);
                    var customName = string.Format(Guid.NewGuid() + fileName);
                    Image.SaveAs(Path.Combine(Server.MapPath("~/NEWIMAGES/"), customName));
                    model.Post.MediaURL = "~/NEWIMAGES/" + customName;
                }
                else
                {
                    ViewBag.ErrorMessage = "Please select an image between 1KB-2MB and in an approved format (.jpg, .bmp, .png, .gif)";



                    model.Post.MediaURL = "images/module-1.jpg";
                }
                model.Post.createdDate = DateTime.Now;

                model.Post.Slug = Slug;
                if (button == "Create")
                {
                    model.Post.Published = false;
                }
                else
                {
                    model.Post.Published = true;
                }

                db.Posts.Add(model.Post);
                db.SaveChanges();

                if (model.Topics != null)
                {
                    foreach (var vm in model.Topics)
                    {
                        int topicId = Convert.ToInt32(vm);
                        helper.AddTopicToBlog(topicId, model.Post.Id);
                    }
                }

                return(RedirectToAction("Index", new { message = ErrorMessage }));
            }
            ViewBag.Topics = new MultiSelectList(db.Topics, "Id", "Description");
            return(View(model));
        }
        public ActionResult Create([Bind(Include = "Post, Topics")] BlogTopicsViewModel model, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                string slug = StringUtilities.URLFriendly(model.Post.Title);
                if (String.IsNullOrWhiteSpace(slug))
                {
                    ModelState.AddModelError("Title", "Invalid title");
                    ViewBag.Topics = new MultiSelectList(db.Topics, "Id", "Description");
                    return(View(model));
                }
                if (db.BlogPosts.Any(p => p.slug == slug))
                {
                    ModelState.AddModelError("Title", "The title must be unique");
                    ViewBag.Topics = new MultiSelectList(db.Topics, "Id", "Description");
                    return(View(model));
                }
                model.Post.Created = DateTimeOffset.Now;
                if (ImageUploadValidator.IsWebFriendlyImage(image))
                {
                    var filename   = Path.GetFileName(image.FileName);
                    var customName = string.Format(Guid.NewGuid() + filename);
                    image.SaveAs(Path.Combine(Server.MapPath("~/assets/images/uploads/"), customName));
                    model.Post.ImageURL = "~/assets/images/uploads/" + customName;
                }
                else
                {
                    ViewBag.Message     = "Please select a valid format image";
                    model.Post.ImageURL = "~/assets/images/post-1.jpg";
                }


                model.Post.slug = slug;
                db.BlogPosts.Add(model.Post);
                db.SaveChanges();

                if (model.Topics != null && model.Topics.Any())
                {
                    foreach (var topic in model.Topics)
                    {
                        int topicId = Convert.ToInt32(topic);
                        helper.AddTopicToBlog(topicId, model.Post.Id);
                    }
                }

                return(RedirectToAction("Single", new { slug = model.Post.slug }));
            }
            ViewBag.Topics = new MultiSelectList(db.Topics, "Id", "Description");
            return(View(model));
        }
Ejemplo n.º 4
0
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            BlogPost blogPost = db.Posts.Find(id);

            if (blogPost == null)
            {
                return(HttpNotFound());
            }

            BlogTopicsViewModel vm = new BlogTopicsViewModel()
            {
                Post = blogPost
            };

            ViewBag.Topics = new MultiSelectList(db.Topics, "Id", "TopicName");
            return(View(vm));
        }
Ejemplo n.º 5
0
        public ActionResult Edit([Bind(Include = "Post, Topics")] BlogTopicsViewModel BlogTopicVM, HttpPostedFileBase image, string button)
        {
            if (ModelState.IsValid)
            {
                var post = db.Posts.AsNoTracking().First(p => p.Id == BlogTopicVM.Post.Id);
                if (BlogTopicVM.Post.Title != post.Title)
                {
                    var Slug = StringUtilities.URLFriendly(BlogTopicVM.Post.Title);
                    if (String.IsNullOrWhiteSpace(Slug))
                    {
                        ModelState.AddModelError("Title", "Invalid title");
                        return(View(BlogTopicVM));
                    }
                    if (db.Posts.Any(p => p.Slug == Slug))
                    {
                        ModelState.AddModelError("Title", "The title must be unique");
                        return(View(BlogTopicVM));
                    }
                    BlogTopicVM.Post.Slug = Slug;
                }
                if (ImageUploadValidator.IsWebFriendlyImage(image))
                {
                    var filePath = Server.MapPath(post.MediaURL);
                    if (filePath != null || filePath != Path.Combine(Server.MapPath("~/ NEWIMAGES/"), "blog.jpg"))
                    {
                        System.IO.File.Delete(filePath);
                    }


                    var fileName   = Path.GetFileName(image.FileName);
                    var customName = string.Format(Guid.NewGuid() + fileName);
                    image.SaveAs(Path.Combine(Server.MapPath("~/NEWIMAGES/"), customName));
                    BlogTopicVM.Post.MediaURL = "~/NEWIMAGES/" + customName;
                }
                if (post.Topics != null)
                {
                    foreach (var vm in post.Topics)
                    {
                        helper.RemoveTopicFromBlog(vm.Id, post.Id);
                    }
                    foreach (var vm in BlogTopicVM.Topics)
                    {
                        int topicId = Convert.ToInt32(vm);
                        helper.AddTopicToBlog(topicId, BlogTopicVM.Post.Id);
                    }
                }

                if (button == "PUBLISH")
                {
                    BlogTopicVM.Post.Published = true;
                }
                else
                {
                    BlogTopicVM.Post.Published = false;
                }
                BlogTopicVM.Post.updatedDate     = DateTime.Now;
                db.Entry(BlogTopicVM.Post).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.Topics = new MultiSelectList(db.Topics, "Id", "TopicName");
            return(View(BlogTopicVM));
        }
        public ActionResult Edit([Bind(Include = "Post, Topics")] BlogTopicsViewModel model, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                var blogPost = db.BlogPosts.AsNoTracking().First(p => p.Id == model.Post.Id);
                if (blogPost.Title != model.Post.Title)
                {
                    string slug = StringUtilities.URLFriendly(model.Post.Title);
                    if (String.IsNullOrWhiteSpace(slug))
                    {
                        ModelState.AddModelError("Title", "Invalid title");
                        ViewBag.Topics = new MultiSelectList(db.Topics, "Id", "Description");
                        return(View(model));
                    }
                    if (db.BlogPosts.Any(p => p.slug == slug))
                    {
                        ModelState.AddModelError("Title", "The title must be unique");
                        ViewBag.Topics = new MultiSelectList(db.Topics, "Id", "Description");
                        return(View(model));
                    }
                }

                if (ImageUploadValidator.IsWebFriendlyImage(image))
                {
                    var filePath        = Server.MapPath(model.Post.ImageURL);
                    var filePathDefault = Server.MapPath("~/assets/images/post-1.jpg");
                    if (System.IO.File.Exists(filePath) && filePath != filePathDefault)
                    {
                        System.IO.File.Delete(filePath);
                    }
                    var filename   = Path.GetFileName(image.FileName);
                    var customName = string.Format(Guid.NewGuid() + filename);
                    image.SaveAs(Path.Combine(Server.MapPath("~/assets/images/uploads/"), customName));
                    model.Post.ImageURL = "~/assets/images/uploads/" + customName;
                }
                else
                {
                    ViewBag.Message = "Please select a valid format image";
                }
                model.Post.Updated         = DateTimeOffset.Now;
                db.Entry(model.Post).State = EntityState.Modified;
                db.SaveChanges();
                if (blogPost.Topics != null && blogPost.Topics.Any())
                {
                    foreach (var topic in blogPost.Topics)
                    {
                        helper.RemoveTopicFromBlog(topic.Id, blogPost.Id);
                    }
                }
                if (model.Topics != null && model.Topics.Any())
                {
                    foreach (var topic in model.Topics)
                    {
                        int topicId = Convert.ToInt32(topic);
                        helper.AddTopicToBlog(topicId, model.Post.Id);
                    }
                }
                return(RedirectToAction("Single", new { slug = model.Post.slug }));
            }

            BlogPost post = db.BlogPosts.Find(model.Post.Id);

            string[] selectedItemIds = new string[post.Topics.Count];
            int      i = 0;

            foreach (var topic in post.Topics)
            {
                selectedItemIds[i] = topic.Id.ToString();
                i++;
            }
            ViewBag.Topics = new MultiSelectList(db.Topics, "Id", "Description", selectedItemIds);
            return(View(model));
        }