コード例 #1
0
        public ActionResult PetGalleryCreate(PetGalleryViewModel model)
        {
            PetGallery newPhoto = new PetGallery();

            if (model.GalleryPhoto != null)
            {
                if (model.GalleryPhoto.ContentLength > (4 * 1024 * 1024))
                {
                    ModelState.AddModelError("CustomError", "Image can not be lager than 4MB.");
                    return(View(model));
                }

                if (!(model.GalleryPhoto.ContentType == "image/jpeg"))
                {
                    ModelState.AddModelError("CustomError", "Image must be in jpeg format.");
                    return(View(model));
                }

                byte[] data = new byte[model.GalleryPhoto.ContentLength];

                model.GalleryPhoto.InputStream.Read(data, 0, model.GalleryPhoto.ContentLength);

                newPhoto.GalleryPhoto = data;
            }

            newPhoto.Comment = model.PhotoComment;
            newPhoto.PetID   = model.CurrentPetID;

            db.PetGallery.Add(newPhoto);
            db.SaveChanges();

            return(RedirectToAction("PetGallery", new { id = model.CurrentPetID }));
        }
コード例 #2
0
        //-------------------------------------------------------------------------------
        //                                                   PET GALLERY - remove a photo
        //-------------------------------------------------------------------------------
        public ActionResult PetGalleryDelete(int id)
        {
            PetGallery petG = db.PetGallery.Find(id);

            int?petID = petG.PetID;

            db.PetGallery.Remove(petG);

            db.SaveChanges();

            return(RedirectToAction("PetGallery", new { id = petID }));
        }