public ActionResult GetImageUploadPreview(string objId)
        {
            int iObjId;
            if (!int.TryParse(objId, out iObjId))
                return RedirectToAction("index", "home");
            PropertyPreview result = new PropertyPreview();

            var smallImageIds = from s in _db.ObjectImages.AsQueryable()
                                where s.PropertyObject.Id == iObjId
                                && s.IsDeleted == false
                                select s.Id;

            foreach (var item in smallImageIds)
            {
                if (result.ImgPreviewIds == null) result.ImgPreviewIds = new List<int>();
                result.ImgPreviewIds.Add(item);
            }

            result.PropertObject = (from s in _db.PropertyObjects
                                        .Include("City")
                       .Include("CityDistrict")
                       .Include("PropertyType")
                       .Include("PropertyAction")
                       .Include("BuildingTypeName")
                       .Include("Currency")
                       .Include("PriceForTypeName")
                       .Include("UserOwner")
                       .Include("WCType")
                       .Include("CommercialPropertyType")
                       .Include("ServiceType")
                       .Include("Periods")
                                    where s.Id == iObjId
                                    select s).SingleOrDefault<PropertyObject>();
            if (result == null || result.PropertObject==null)
                return RedirectToAction("index", "home");

            if (result.PropertObject != null && result.PropertObject.PropertyType.Id == 3)
            {
                var landCommunication = (from s in _db.LandCommunicationToObjs
                                         .Include("LandCommunicationId")
                                         where s.PropertyObjectId.Id==iObjId
                                         select s.LandCommunicationId).ToList<LandCommunication>();
                ViewBag.landCommunication = landCommunication;

                var landFunction = (from s in _db.LandFunctionToObjs
                                    .Include("LandFunctionId")
                                    where s.PropertyObjectId.Id == iObjId
                                    select s.LandFunctionId).ToList<LandFunction>();
                ViewBag.landFunction = landFunction;
            }
            return View(result);
        }
        public ActionResult UploadImage(int objId, HttpPostedFileBase file = null)
        {
            // Verify that the user selected a file
            if (file != null && file.ContentLength > 0)
            {
                // extract only the fielname
                // var fileName = Path.GetFileName(file.FileName);
                // store the file inside ~/App_Data/uploads folder
                // var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                // file.SaveAs(path);


                var propObj = (from s in _db.PropertyObjects
                       .Include("City")
                       .Include("CityDistrict")
                       .Include("PropertyType")
                       .Include("PropertyAction")
                       .Include("BuildingTypeName")
                       .Include("Currency")
                       .Include("PriceForTypeName")
                       .Include("UserOwner")
                       .Include("WCType")
                       .Include("CommercialPropertyType")
                       .Include("ServiceType")
                       .Include("Periods")
                               where s.Id == objId
                               select s).SingleOrDefault<PropertyObject>();

                ImageProcessing imgHelper = new ImageProcessing();
                using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream))
                {
                    //Initialise the size of the array
                    byte[] f = new byte[file.InputStream.Length];
                    //Create a new BinaryReader and set the InputStream  for the Images InputStream to the beginning, as we create the img using a stream.
                    var reader = new BinaryReader(file.InputStream);
                    file.InputStream.Seek(0, SeekOrigin.Begin);
                    //Load the image binary.
                    f = reader.ReadBytes((int)file.InputStream.Length);
                    //Create a new image to be added to the database
                    if (img.Width > 512)
                        f = imgHelper.ResizeImage(f, new System.Drawing.Size(512, 389));
                    ObjectImages objImages = new ObjectImages()
                    {
                        Content_Type = file.ContentType,
                        FileName = file.FileName,
                        FileSize = file.ContentLength,
                        Height = (img.Width > 512 ? 512 : img.Height),
                        Width = (img.Width > 512 ? 512 : img.Width),
                        Image = f,
                        ImagePreview = imgHelper.ResizeImage(f, new System.Drawing.Size(120, 90)),
                        PropertyObject = propObj,
                        IsDeleted = false
                    };

                    _db.ObjectImages.Add(objImages);
                    _db.Entry(objImages.PropertyObject).State = System.Data.EntityState.Unchanged;
                    _db.SaveChanges();
                }
                var countImages = (from s in _db.ObjectImages
                                   where s.PropertyObject.Id == propObj.Id
                                   && s.IsDeleted == false
                                   select 1).Count<int>();

                propObj.CountPhotos = countImages;
                _db.PropertyObjects.Attach(propObj);
                _db.Entry(propObj).Property(x => x.CountPhotos).IsModified = true;
                _db.SaveChanges();
            }
            PropertyPreview result = new PropertyPreview();

            var smallImageIds = from s in _db.ObjectImages.AsQueryable()
                                where s.PropertyObject.Id == objId
                                && s.IsDeleted == false
                                select s.Id;

            foreach (var item in smallImageIds)
            {
                if (result.ImgPreviewIds == null) result.ImgPreviewIds = new List<int>();
                result.ImgPreviewIds.Add(item);
            }

            result.PropertObject = (from s in _db.PropertyObjects
                                    where s.Id == objId
                                    select s).Single<PropertyObject>();

            return RedirectToAction("GetImageUploadPreview", new { objId = objId });
            //ViewBag.ObjId = objId;
            //return View(result);


            // redirect back to the index action to show the form once again
        }