예제 #1
0
        public ActionResult Create(Article article)
        {
            if (ModelState.IsValid)
            {
                int id = GetNextId();

                var sourcePath = Path.Combine(Server.MapPath("~/Images"), "temp");
                var destPath = Path.Combine(Server.MapPath("~/Images/FeaturedImages"), id.ToString());

                var success = MoveTempFeaturedImage(sourcePath, destPath);
                CreateThumbnail(sourcePath, destPath, success);

                article.ImageUrl = "~/Images/FeaturedImages/" + id.ToString();
                article.LastModifiedDate = DateTime.Now;
                article.PublishedDate = DateTime.Now;
                article.Published = false;
                db.ArticleSet.Add(article);
                db.SaveChanges();

                SendMessage(MessageType.Success, "The article successfully created!");
                return RedirectToAction("Index");
            }

            SendMessage(MessageType.Error, "Something went wrong! :( Try it again");
            return View(article);
        }
예제 #2
0
        public ActionResult Create(Article article)
        {
            if (ModelState.IsValid)
            {
                int id;
                try
                {
                    id = db.ArticleSet.Select(e => e.Id).OrderByDescending(e => e).First() + 1;
                }
                catch (Exception)
                {
                    id = 0;
                }

                var sourcePath = Path.Combine(Server.MapPath("~/Images"), "temp");
                var destPath = Path.Combine(Server.MapPath("~/Images/FeaturedImages"), id.ToString());

                var success = (new UploadController()).SaveFeaturedImage(sourcePath, destPath);
                if (!success)
                {
                    sourcePath = Path.Combine(Server.MapPath("~/Images"), "no_image");
                    System.IO.File.Copy(sourcePath, destPath);
                }

                article.ImageUrl = "~/Images/FeaturedImages/" + id.ToString();
                article.LastModifiedDate = DateTime.Now;
                article.PublishedDate = DateTime.Now;
                article.Published = false;
                db.ArticleSet.Add(article);
                db.SaveChanges();

                SendMessage(MessageType.Success, "The article successfully created!");
                return RedirectToAction("Index");
            }

            SendMessage(MessageType.Error, "Something went wrong! :( Try it again");
            return View(article);
        }
예제 #3
0
        public ActionResult Edit(Article article)
        {
            if (ModelState.IsValid)
            {
                var sourcePath = Path.Combine(Server.MapPath("~/Images"), "temp");
                var destPath = Path.Combine(Server.MapPath("~/Images/FeaturedImages"), article.Id.ToString());

                if (System.IO.File.Exists(sourcePath) && System.IO.File.Exists(destPath))
                {
                    System.IO.File.Delete(destPath);
                }
                (new UploadController()).SaveFeaturedImage(sourcePath, destPath);

                article.LastModifiedDate = DateTime.Now;
                db.Entry(article).State = EntityState.Modified;
                db.SaveChanges(); SendMessage(MessageType.Success, "The article successfully modified!");
                return RedirectToAction("Index");
            }
            SendMessage(MessageType.Error, "Something went wrong! :( Try it again");
            return View(article);
        }