Ejemplo n.º 1
0
        public ActionResult DeleteConfirmed(string id)
        {
            AspNetRoles aspNetRoles = db.AspNetRoles.Find(id);

            db.AspNetRoles.Remove(aspNetRoles);
            db.SaveChanges();
            //return RedirectToAction("Index");
            return(Json(new { ok = "true" }));
        }
Ejemplo n.º 2
0
        public ActionResult Create([Bind(Include = "ID,Nombre,Telefono")] Guia guia)
        {
            if (ModelState.IsValid)
            {
                db.Guia.Add(guia);
                db.SaveChanges();
                return(Json(new
                {
                    ok = true
                }));
            }

            return(View(guia));
        }
Ejemplo n.º 3
0
        //[ValidateAntiForgeryToken]
        public ActionResult Create(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);

            try
            {
                if (ModelState.IsValid)
                {
                    db.Sendero.Add(sendero);
                    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(senderoImg.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
                                    senderoImg.SaveAs(fullPhisicalPath);

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

                    return(Json(new
                    {
                        ok = true
                    }));
                }
            }
            catch (DbEntityValidationException ex)
            {
                foreach (var entityValidationErrors in ex.EntityValidationErrors)
                {
                    foreach (var validationError in entityValidationErrors.ValidationErrors)
                    {
                        Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
                    }
                }

                //return Json(new
                //{
                //    ok = false,
                //    msj= ex.InnerException.Message
                //});
            }



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


            return(View(sendero));
        }