Exemplo n.º 1
0
        public ActionResult UpdateBlog(BlogEditFormViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View("BlogEditForm", viewModel));
            }
            if (viewModel.FeatureImage != null)
            {
                string fileName  = Path.GetFileNameWithoutExtension(viewModel.FeatureImage.FileName);
                string extension = Path.GetExtension(viewModel.FeatureImage.FileName);
                fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
                viewModel.BlogPost.FeatureImagePath = "~/Images/" + fileName;
                fileName = Path.Combine(Server.MapPath("~/Images"), fileName);
                viewModel.FeatureImage.SaveAs(fileName);
            }

//            viewModel.BlogPost.AuthorId = User.Identity.GetUserId();
            var blogPostInDb = _context.Blogs.SingleOrDefault(b => b.Id == viewModel.BlogPost.Id);

//            _context.Blogs.Add(viewModel.BlogPost);
            blogPostInDb.Content          = viewModel.BlogPost.Content;
            blogPostInDb.Title            = viewModel.BlogPost.Title;
            blogPostInDb.FeatureImagePath = viewModel.BlogPost.FeatureImagePath;
            _context.SaveChanges();
            return(RedirectToAction("Blog", "Admin"));
        }
Exemplo n.º 2
0
        public ActionResult EditBlog(int id)
        {
            var blogpost = _context.Blogs.Include(b => b.Author).SingleOrDefault(b => b.Id == id);

            if (blogpost != null)
            {
                var viewModel = new BlogEditFormViewModel
                {
                    BlogPost = blogpost
                };
                return(View("BlogEditForm", viewModel));
            }

            return(RedirectToAction("Blog"));
        }