Пример #1
0
        public ActionResult Create([Bind(Include = "id,title,photo,description,type_id")] post_galery post_galery, HttpPostedFileBase photo)
        {
            if (ModelState.IsValid)
            {
                if (photo == null)
                {
                    Session["uploadError"] = "Your must select your file";
                    return(RedirectToAction("create"));
                }
                if (photo.ContentType != "image/png" && photo.ContentType != "image/jpeg" && photo.ContentType != "image/gif")
                {
                    Session["uploadError"] = "Your file must be jpg,png or gif";
                    return(RedirectToAction("create"));
                }
                if ((photo.ContentLength / 1024) > 1024)
                {
                    Session["uploadError"] = "Your file size must be max 1mb";
                    return(RedirectToAction("create"));
                }
                string filename = DateTime.Now.ToString("ddMMyyyyHHmmssffff") + photo.FileName;
                string path     = Path.Combine(Server.MapPath("~/Uploads"), filename);
                photo.SaveAs(path);
                post_galery.photo = filename;
                db.post_galery.Add(post_galery);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.type_id = new SelectList(db.Type_blog, "id", "name", post_galery.type_id);
            return(View(post_galery));
        }
Пример #2
0
        public ActionResult DeleteConfirmed(int id)
        {
            post_galery post_galery = db.post_galery.Find(id);

            db.post_galery.Remove(post_galery);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Пример #3
0
        // GET: Back/post_galery/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            post_galery post_galery = db.post_galery.Find(id);

            if (post_galery == null)
            {
                return(HttpNotFound());
            }
            return(View(post_galery));
        }
Пример #4
0
        // GET: Back/post_galery/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            post_galery post_galery = db.post_galery.Find(id);

            if (post_galery == null)
            {
                return(HttpNotFound());
            }
            ViewBag.type_id = new SelectList(db.Type_blog, "id", "name", post_galery.type_id);
            return(View(post_galery));
        }
Пример #5
0
        public ActionResult Edit([Bind(Include = "id,title,photo,description,type_id")] post_galery post_galery, HttpPostedFileBase photo, string OldPhoto)
        {
            if (ModelState.IsValid)
            {
                if (photo != null)
                {
                    if (photo.ContentType != "image/png" && photo.ContentType != "image/jpg" && photo.ContentType != "image/gif" && photo.ContentType != "image/jpeg")
                    {
                        Session["uploadError"] = "your file must be jpg, png, gif, jpeg";
                        return(RedirectToAction("update", "post_galery", new { id = post_galery.id }));
                    }
                    if ((photo.ContentLength / 1024) > 1024)
                    {
                        Session["uploadError"] = "your file size must be max 1mb";
                        return(RedirectToAction("update", "post_galery", new { id = post_galery.id }));
                    }

                    string FileDate = DateTime.Now.ToString("ddMMyyyHHmmssffff") + photo.FileName;
                    string path     = Path.Combine(Server.MapPath("~/Uploads"), FileDate);
                    string oldpath  = Path.Combine(Server.MapPath("~/Uploads"), OldPhoto);
                    if (System.IO.File.Exists(oldpath))
                    {
                        System.IO.File.Delete(oldpath);
                    }
                    photo.SaveAs(path);
                    post_galery.photo = FileDate;
                }
                else
                {
                    post_galery.photo = OldPhoto;
                }
                db.Entry(post_galery).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.type_id = new SelectList(db.Type_blog, "id", "name", post_galery.type_id);
            return(View(post_galery));
        }