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

            using (var db = new BlogDbContext())
            {
                var skizone = db.Skizones.Where(a => a.Id == id).First();

                if (!IsUserAuthorizedToEdit(skizone))
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
                }

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

                var model = new SkizoneViewModel();
                model.Id            = skizone.Id;
                model.Name          = skizone.Name;
                model.ElevationInfo = skizone.ElevationInfo;
                model.Slopes        = skizone.Slopes;
                model.LiftTicket    = skizone.LiftTicket;
                model.ImagePath     = skizone.ImagePath;
                model.ContentInfo   = skizone.ContentInfo;
                model.CategoryId    = skizone.CategoryId;
                model.Categories    = db.Categories.OrderBy(c => c.Name).ToList();
                return(View(model));
            }
        }
 public ActionResult Create()
 {
     using (var db = new BlogDbContext())
     {
         var model = new SkizoneViewModel();
         model.Categories = db.Categories.OrderBy(c => c.Name).ToList();
         return(View(model));
     }
 }
        public ActionResult Edit(SkizoneViewModel model, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                using (var db = new BlogDbContext())
                {
                    var skizone = db.Skizones.FirstOrDefault(a => a.Id == model.Id);

                    if (!IsUserAuthorizedToEdit(skizone))
                    {
                        return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
                    }

                    if (image != null)
                    {
                        var allowedContentTypes = new[]
                        {
                            "image/jpeg", "image/jpg", "image/png"
                        };
                        if (allowedContentTypes.Contains(image.ContentType))
                        {
                            var imagesPath   = "/Content/Images/";
                            var filename     = image.FileName;
                            var uploadPath   = imagesPath + filename;
                            var physicalPath = Server.MapPath(uploadPath);
                            image.SaveAs(physicalPath);
                            model.ImagePath = uploadPath;
                        }
                    }

                    skizone.Name            = model.Name;
                    skizone.ElevationInfo   = model.ElevationInfo;
                    skizone.Slopes          = model.Slopes;
                    skizone.LiftTicket      = model.LiftTicket;
                    skizone.ContentInfo     = model.ContentInfo;
                    skizone.ImagePath       = model.ImagePath;
                    skizone.CategoryId      = model.CategoryId;
                    db.Entry(skizone).State = EntityState.Modified;
                    db.SaveChanges();

                    return(RedirectToAction("Details", new { id = model.Id }));
                }
            }

            return(View(model));
        }
        public ActionResult Create(SkizoneViewModel model, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                using (var db = new BlogDbContext())
                {
                    var authorId = User.Identity.GetUserId();
                    model.AuthorId = authorId;

                    if (image != null)
                    {
                        var allowedContentTypes = new[]
                        {
                            "image/jpeg", "image/jpg", "image/png"
                        };
                        if (allowedContentTypes.Contains(image.ContentType))
                        {
                            var imagesPath   = "/Content/Images/";
                            var filename     = image.FileName;
                            var uploadPath   = imagesPath + filename;
                            var physicalPath = Server.MapPath(uploadPath);
                            image.SaveAs(physicalPath);
                            model.ImagePath = uploadPath;
                        }
                    }

                    var skizone = new Skizone(authorId, model.Name, model.ElevationInfo, model.CategoryId, model.Slopes, model.LiftTicket, model.ContentInfo, model.ImagePath);
                    db.Skizones.Add(skizone);
                    db.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }

            return(View(model));
        }