コード例 #1
0
        public ActionResult Create([Bind(Include = "VehiculoId,CategoriaId,MarcaId,Modelo,TransmisionId,ColorId,PrecioVenta,PrecioCompra,Descripcion,Activo,NombreVehiculo,MarcarComoOferta,PrecioOFerta")] NE_Vehiculo nE_Vehiculo)
        {
            try
            {
                string             mensaje;
                HttpPostedFileBase postedFile = Request.Files[0];

                if (ModelState.IsValid)
                {
                    if (postedFile != null && postedFile.ContentLength > 0)
                    {
                        IList <string> AllowedFileExtensions = new List <string> {
                            ".jpg", ".gif", ".png"
                        };
                        var ext       = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.'));
                        var extension = ext.ToLower();
                        if (!AllowedFileExtensions.Contains(extension))
                        {
                            mensaje = "Porfavor actualiza la imagen a estension de tipo .jpg,.gif,.png.";
                        }
                        else
                        {
                            db.NE_Vehiculo.Add(nE_Vehiculo);
                            db.SaveChanges();

                            var name = String.Format("Vehiculo_{0}", nE_Vehiculo.VehiculoId);
                            //var filePath = Server.MapPath("~/Imagenes/Productos/" + postedFile.FileName + extension);
                            var filePath = Server.MapPath("~/Imagenes/Vehiculo/" + name + extension);

                            NE_VehiculoImagen imagen = new NE_VehiculoImagen()
                            {
                                Extension  = extension,
                                Nombre     = name,
                                VehiculoId = nE_Vehiculo.VehiculoId,
                                Ruta       = filePath,
                            };
                            db.NE_VehiculoImagen.Add(imagen);
                            db.SaveChanges();

                            postedFile.SaveAs(filePath);

                            return(RedirectToAction("Index"));
                        }
                    }
                }
                ViewBag.CategoriaId   = new SelectList(db.NE_Categoria, "CategoriaId", "Categoria", nE_Vehiculo.CategoriaId);
                ViewBag.ColorId       = new SelectList(db.NE_Color, "ColorId", "Color", nE_Vehiculo.ColorId);
                ViewBag.MarcaId       = new SelectList(db.NE_Marca, "MarcaId", "Marca", nE_Vehiculo.MarcaId);
                ViewBag.TransmisionId = new SelectList(db.NE_Transmision, "TransmisionId", "Transmision", nE_Vehiculo.TransmisionId);
                return(View(nE_Vehiculo));
            }
            catch (Exception ex)
            {
                return(View(nE_Vehiculo));
            }
        }
コード例 #2
0
        public ActionResult DeleteConfirmed(int id)
        {
            NE_VehiculoImagen nE_VehiculoImagen = new NE_VehiculoImagen();
            NE_Vehiculo       nE_Vehiculo       = db.NE_Vehiculo.Find(id);

            db.NE_Vehiculo.Remove(nE_Vehiculo);
            nE_VehiculoImagen = db.NE_VehiculoImagen.Where(x => x.VehiculoId == id).First();
            db.NE_VehiculoImagen.Remove(nE_VehiculoImagen);
            db.SaveChanges();
            System.IO.File.Delete(nE_VehiculoImagen.Ruta);
            return(RedirectToAction("Index"));
        }
コード例 #3
0
        public ActionResult ImagenCreate(int id)
        {
            try
            {
                string             mensaje;
                HttpPostedFileBase postedFile = Request.Files[0];

                if (postedFile != null && postedFile.ContentLength > 0)
                {
                    IList <string> AllowedFileExtensions = new List <string> {
                        ".jpg", ".gif", ".png"
                    };
                    var ext       = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.'));
                    var extension = ext.ToLower();
                    if (!AllowedFileExtensions.Contains(extension))
                    {
                        mensaje = "Porfavor actualiza la imagen a estension de tipo .jpg,.gif,.png.";
                    }
                    else
                    {
                        var name = String.Format("Vehiculo_{0:ddMMyyyyHHmmss}", DateTime.Now);
                        //var filePath = Server.MapPath("~/Imagenes/Productos/" + postedFile.FileName + extension);
                        var filePath = Server.MapPath("~/Imagenes/Vehiculo/" + name + extension);

                        NE_VehiculoImagen imagen = new NE_VehiculoImagen()
                        {
                            Extension  = extension,
                            Nombre     = name,
                            VehiculoId = id,
                            Ruta       = filePath,
                        };
                        db.NE_VehiculoImagen.Add(imagen);
                        db.SaveChanges();


                        postedFile.SaveAs(filePath);

                        return(RedirectToAction("Index"));
                    }
                }

                return(View());
            }
            catch (Exception ex)
            {
                return(View());
            }
        }
コード例 #4
0
        public ActionResult DeleteImagesPost(Int32 id)
        {
            NE_VehiculoImagen prodImagen = db.NE_VehiculoImagen.Find(id);
            Int32             ID_Base    = prodImagen.VehiculoId;

            if (System.IO.File.Exists(Server.MapPath("~/Imagenes/Producto/" + prodImagen.Nombre + prodImagen.Extension)))
            {
                System.IO.File.Delete(Server.MapPath("~/Imagenes/Producto/" + prodImagen.Nombre + prodImagen.Extension));
            }


            db.NE_VehiculoImagen.Remove(prodImagen);
            db.SaveChanges();

            return(RedirectToAction("DeleteImages", new { id = ID_Base }));
        }