Example #1
0
        public ActionResult Create(PropertyView model)
        {
            if (ModelState.IsValid)
            {
                int dId = AdminService.AddDetails(model.Details.Price, model.Details.SpaceM2,
                                                  model.Details.MoreDetails, model.Details.RoomsId, model.Property.ID);
                int aId = AdminService.AddAddress(model.Address.City, model.Address.Street,
                                                  model.Address.Block, model.Address.Apartment);

                // TODO:add status


                bool   isSavedSuccessfully = true;
                string fName = "";
                try
                {
                    foreach (string fileName in Request.Files)
                    {
                        HttpPostedFileBase file = Request.Files[fileName];
                        //Save file content goes here
                        fName = file.FileName;
                        if (file != null && file.ContentLength > 0)
                        {
                            var originalDirectory = new DirectoryInfo(string.Format("{0}Images\\WallImages", Server.MapPath(@"\")));

                            string pathString = System.IO.Path.Combine(originalDirectory.ToString(), "imagepath");

                            var fileName1 = Path.GetFileName(file.FileName);

                            bool isExists = System.IO.Directory.Exists(pathString);

                            if (!isExists)
                            {
                                System.IO.Directory.CreateDirectory(pathString);
                            }

                            var path = string.Format("{0}\\{1}", pathString, file.FileName);
                            file.SaveAs(path);

                            int pId = AdminService.AddPhoto(path);
                            AdminService.AddProperty(model.Property.Name, aId, dId, pId);
                        }
                    }
                }
                catch (Exception ex)
                {
                    isSavedSuccessfully = false;
                }


                if (isSavedSuccessfully)
                {
                    return(View("Index"));
                }
                else
                {
                    return(Json(new { Message = "Error in saving file" }));
                }
            }
            return(View("Index"));
        }