Esempio n. 1
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));
        }