public ActionResult Create(HttpPostedFileBase[] files, string[] descriptions)
        {
            //Create Post
            Post post = new Post();
            post.title = Request["post.title"];
            post.slug = Request["post.slug"];
            post.type = PostType.Image;
            post.isFeature = bool.Parse(Request["post.isFeature"]);
            post.imageUrl = Request["post.imageUrl"];
            post.user = db.Users.Find(User.Identity.GetUserId());
            post.content = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
            post.createdDate = DateTime.Now;
            post.category = db.categories.Find(int.Parse(Request["post.category"]));

            db.posts.Add(post);
            db.SaveChanges();

            string id = post.id.ToString();

            if (files.Count() > 0)
            {

                //Create ImageGallery
                ImageGallery imageGallery = new ImageGallery();
                imageGallery.Id = Guid.NewGuid();
                imageGallery.Name = Request["imageGallery.Name"];
                imageGallery.ImagePath = "~/ImageGallery/";
                imageGallery.post = post;
                imageGallery = db.imageGallery.Add(imageGallery);
                db.SaveChanges();

                //Create List of Image for ImageGallery has been created
                for (int i = 0; i < files.Count(); i++)
                {
                    HttpPostedFileBase file = files[i];
                    string guid = Guid.NewGuid().ToString();
                    string fileName = imageGallery.Id + "_" + i + "_" + file.FileName;
                    file.SaveAs(Server.MapPath("~/ImageGallery/" + fileName));
                    Images image = new Images();
                    image.Description = descriptions[i] ?? "";
                    image.order = i;
                    image.gallery = imageGallery;
                    image.Name = i.ToString();
                    image.Path = imageGallery.ImagePath + fileName;
                    db.images.Add(image);

                    imageGallery.ImageList.Add(image);

                    //string path = "https://"+Request.Url.Host+":"+Request.Url.Port+ VirtualPathUtility.ToAbsolute(image.Path);
                }

                db.SaveChanges();
            }

            return View("index");
        }
        public ActionResult Edit(HttpPostedFileBase[] files, string[] updateImages)
        {
            List<UpdateImageModel> updateImageModels = new List<UpdateImageModel>();
            List<int> oldOrders = new List<int>();
            if (updateImages != null)
            {
                foreach (var s in updateImages)
                {
                    UpdateImageModel updateImageModel = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<UpdateImageModel>(s);
                    updateImageModels.Add(updateImageModel);
                }
            }

            string imageGallery_id = Request["imageGallery.Id"];
            ImageGallery imageGallery = db.imageGallery.Where(i => i.Id.ToString() == imageGallery_id).FirstOrDefault();
            IEnumerable<UpdateImageModel> oldImages = updateImageModels.Where(i => i.isOld == true);
            int post_id = int.Parse(Request["post.id"]);
            Post post = db.posts.Where(p => p.id == post_id).FirstOrDefault();
            if (imageGallery != null)
            {

                //Update Old Image and Remove Old Image
                if (oldImages.Count() > 0)
                {
                    //update oldImages (description and order)
                    foreach (var item in oldImages)
                    {
                        Images image = imageGallery.ImageList.Where(j => j.order == item.oldOrder).FirstOrDefault();
                        if (image != null)
                        {
                            image.order = item.newOrder;
                            image.Description = item.description;
                            db.SaveChanges();
                        }

                    }
                }

                //remove image
                IEnumerable<int> removeOrders = imageGallery.ImageList.Select(i => i.order).Except(oldImages.Select(o => o.oldOrder)).ToArray();
                foreach (var i in removeOrders)
                {

                    Images image = imageGallery.ImageList.Where(j => j.order == i).FirstOrDefault();
                    if (image != null)
                    {
                        string path = Server.MapPath(image.Path);
                        if (System.IO.File.Exists(path))
                        {
                            System.IO.File.Delete(path);
                        }

                        imageGallery.ImageList.Remove(image);
                        db.images.Remove(image);
                    }

                }
                db.SaveChanges();

                //Update Post

                if (post != null)
                {
                    post.title = Request["post.title"];
                    post.slug = Request["post.slug"];
                    post.type = PostType.Image;
                    post.isFeature = bool.Parse(Request["post.isFeature"]);
                    post.imageUrl = Request["post.imageUrl"];
                    post.user = db.Users.Find(User.Identity.GetUserId());
                    post.content = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
                    post.createdDate = DateTime.Now;
                    post.category = db.categories.Find(int.Parse(Request["post.category"]));

                    db.SaveChanges();
                }

                if (files != null)
                {
                    IEnumerable<UpdateImageModel> newImages = updateImageModels.Where(i => i.isOld == false);
                    if (files.Count() > 0)
                    {
                        //update Image Gallery and Add Image to  ImageGallery
                        imageGallery.Name = Request["imageGallery.Name"];
                        //imageGallery.ImagePath = "~/ImageGallery/";

                        //Create List of Image for ImageGallery has been created
                        int i = 0;
                        foreach (var item in newImages)
                        {
                            HttpPostedFileBase file = files[i];
                            string guid = Guid.NewGuid().ToString();
                            string fileName = imageGallery.Id + "_" + item.newOrder + "_" + file.FileName;
                            file.SaveAs(Server.MapPath("~/ImageGallery/" + fileName));
                            Images image = new Images();
                            image.Description = item.description;
                            image.gallery = imageGallery;
                            image.order = item.newOrder;
                            image.Name = item.newOrder.ToString();
                            image.Path = imageGallery.ImagePath + fileName;
                            db.images.Add(image);

                            imageGallery.ImageList.Add(image);

                            db.SaveChanges();
                            i++;
                            //string path = "https://"+Request.Url.Host+":"+Request.Url.Port+ VirtualPathUtility.ToAbsolute(image.Path);
                        }
                    }
                }

                db.SaveChanges();
                //return RedirectToAction("Index");
            }

            return View(new ImageGalleryPostViewModel() { imageGallery = imageGallery, post = post });
        }