Esempio n. 1
0
 public ActionResult Edit([Bind(Include = "ID,Nombre,Telefono")] Guia guia)
 {
     if (ModelState.IsValid)
     {
         db.Entry(guia).State = EntityState.Modified;
         db.SaveChanges();
         return(Json(new
         {
             ok = true
         }));
     }
     return(View(guia));
 }
Esempio n. 2
0
        public ActionResult Edit(Sendero sendero, HttpPostedFileBase senderoImg, HttpPostedFileBase zipMapa)//[Bind(Include = "ID,Nombre,Descripcion,LugarInicio,LugarFin,TipoDificultadTecnicaID,TipoDificultadFisicaID,Desnivel,Distancia,AlturaMaxima,DuracionTotal")]
        {
            sendero = JsonConvert.DeserializeObject <Sendero>(Request.Form["Sendero"]);
            //var x =Request.Files.Count;
            //var ssendero = Request.Form["Sendero"];
            //sendero.ID = 2;//dato de prueba

            ModelState["sendero"].Errors.Clear();
            UpdateModel <Sendero>(sendero);


            if (ModelState.IsValid)
            {
                #region Delete and Add "SenderoPunto"
                if (db.SenderoPunto.Any(r => r.SenderoID == sendero.ID))
                {
                    //Delete old objects
                    db.SenderoPunto.RemoveRange(db.SenderoPunto.Where(r => r.SenderoID == sendero.ID));

                    //Add new objects
                    sendero.SenderoPunto.ToList().ForEach(r => r.SenderoID = sendero.ID);
                    db.SenderoPunto.AddRange(sendero.SenderoPunto);
                }
                else
                {
                    //Add new objects
                    sendero.SenderoPunto.ToList().ForEach(r => r.SenderoID = sendero.ID);
                    db.SenderoPunto.AddRange(sendero.SenderoPunto);
                }
                #endregion


                #region Delete and Add "SenderoPuntoElevacion"
                if (db.SenderoPuntoElevacion.Any(r => r.SenderoID == sendero.ID))
                {
                    //Delete old objects
                    db.SenderoPuntoElevacion.RemoveRange(db.SenderoPuntoElevacion.Where(r => r.SenderoID == sendero.ID));

                    //Add new objects
                    sendero.SenderoPuntoElevacion.ToList().ForEach(r => r.SenderoID = sendero.ID);
                    db.SenderoPuntoElevacion.AddRange(sendero.SenderoPuntoElevacion);
                }
                else
                {
                    //Add new objects
                    sendero.SenderoPuntoElevacion.ToList().ForEach(r => r.SenderoID = sendero.ID);
                    db.SenderoPuntoElevacion.AddRange(sendero.SenderoPuntoElevacion);
                }
                #endregion


                #region Delete and Add "SenderoPuntoInteres"
                if (db.SenderoPuntoInteres.Any(r => r.SenderoID == sendero.ID))
                {
                    //Delete old objects
                    db.SenderoPuntoInteres.RemoveRange(db.SenderoPuntoInteres.Where(r => r.SenderoID == sendero.ID));

                    //Add new objects
                    sendero.SenderoPuntoInteres.ToList().ForEach(r => r.SenderoID = sendero.ID);
                    db.SenderoPuntoInteres.AddRange(sendero.SenderoPuntoInteres);
                }
                else
                {
                    //Add new objects
                    sendero.SenderoPuntoInteres.ToList().ForEach(r => r.SenderoID = sendero.ID);
                    db.SenderoPuntoInteres.AddRange(sendero.SenderoPuntoInteres);
                }
                #endregion


                db.Entry(sendero).State = EntityState.Modified;
                db.SaveChanges();


                if (Request.Files.Count > 0)
                {
                    //Ej. ruta de recursos(imagen) de senderos: "~/Content/Senderos/2/Img/senderoImg_2.jpg"
                    //Ej. ruta de recursos(mapa offline) de senderos: "~/Content/Senderos/2/Mapa/senderoMapa_2.zip"

                    //SenderoImg
                    #region process SenderoImg
                    if (senderoImg != null)
                    {
                        if (senderoImg.ContentLength > 0)
                        {
                            //Validate file extension
                            string fileExtension = Path.GetExtension(senderoImg.FileName);
                            if (new[] { ".jpg", ".png" }.Any(c => fileExtension == c))
                            {
                                //Parse image file to base64 encode
                                MemoryStream target = new MemoryStream();
                                senderoImg.InputStream.CopyTo(target);
                                byte[] data = target.ToArray();

                                var ImgBase64 = Convert.ToBase64String(data);



                                //Create name and paths
                                string fileName              = "senderoImg_" + sendero.ID + Path.GetExtension(senderoImg.FileName); //Ej: "senderoiImg_2.jpg"
                                string virtualDirectoryPath  = "~/Content/Senderos/" + sendero.ID + "/Img";                         //Ej: "~/Content/Senderos/2/Img/"
                                string phisicaldirectoryPath = Server.MapPath(virtualDirectoryPath);                                //Ej: "F:/Sistemas/GitHub-Repositorios/TurApp-BackOffice/TurApp/MSP/Content/Senderos/2/Img"
                                string fullVirtualPath       = virtualDirectoryPath + "/" + fileName;
                                string fullPhisicalPath      = Path.Combine(phisicaldirectoryPath, fileName);                       //Ej: "~/Content/Senderos/2/Img/senderoiImg_2.jpg"

                                //CreateDirectory
                                System.IO.Directory.CreateDirectory(phisicaldirectoryPath);

                                //Save file in a phisicaldirectoryPath
                                senderoImg.SaveAs(fullPhisicalPath);

                                //Update in DB Sendero file path
                                sendero.ImgBase64  = "data:image/jpeg;base64," + ImgBase64;
                                sendero.RutaImagen = fullVirtualPath.Substring(1, fullVirtualPath.Length - 1);
                                db.SaveChanges();
                            }
                        }
                    }
                    #endregion

                    //ZipMapa
                    #region process ZipMapa
                    if (zipMapa != null)
                    {
                        if (zipMapa.ContentLength > 0)
                        {
                            //Validate file extension
                            string fileExtension = Path.GetExtension(zipMapa.FileName);
                            if (new[] { ".zip" }.Any(c => fileExtension == c))
                            {
                                //Create name and paths
                                string fileName              = "senderoMapa_" + sendero.ID + Path.GetExtension(zipMapa.FileName); //Ej: "senderoiMapa_2.jpg"
                                string virtualDirectoryPath  = "~/Content/Senderos/" + sendero.ID + "/Mapa";                      //Ej: "~/Content/Senderos/2/Mapa/"
                                string phisicaldirectoryPath = Server.MapPath(virtualDirectoryPath);
                                string fullVirtualPath       = virtualDirectoryPath + "/" + fileName;
                                string fullPhisicalPath      = Path.Combine(phisicaldirectoryPath, fileName);               //Ej: "~/Content/Senderos/2/Img/senderoiImg_2.jpg"

                                //CreateDirectory
                                System.IO.Directory.CreateDirectory(phisicaldirectoryPath);

                                //Save file in a phisicaldirectoryPath
                                zipMapa.SaveAs(fullPhisicalPath);

                                //Update in DB Sendero file path
                                sendero.RutZipMapa = fullVirtualPath.Substring(1, fullVirtualPath.Length - 1);//fullVirtualPath;
                                db.SaveChanges();
                            }
                        }
                    }
                    #endregion
                }


                return(Json(new
                {
                    ok = true
                }));
            }


            ViewBag.TipoDificultadFisicaID  = new SelectList(db.TipoDificultadFisica, "ID", "Descripcion", sendero.TipoDificultadFisicaID);
            ViewBag.TipoDificultadTecnicaID = new SelectList(db.TipoDificultadTecnica, "ID", "Descripcion", sendero.TipoDificultadTecnicaID);
            ViewBag.SenderoSectorID         = new SelectList(db.SenderoSector, "ID", "Nombre", "NombreDepartamento", sendero.SenderoSectorID);
            return(View(sendero));
        }