public ActionResult EliminarNoticia(int id) { noticias noticia = db.noticias.Find(id); int n = noticia.id_noticia; db.noticias.Remove(noticia); db.SaveChanges(); var imagenes = db.imagenes.ToList(); int idImagen = 0; foreach (var i in imagenes) { if (i.id_noticia == n) { idImagen = i.id_imagen; } } imagenes img = db.imagenes.Find(idImagen); if (img != null) { db.imagenes.Remove(img); db.SaveChanges(); String ruta = Path.Combine(Server.MapPath("/Imagenes/"), img.nombre); System.IO.File.Delete(ruta); } return(RedirectToAction("Noticias")); }
public imagenes Get(int id) { ImagenesDAO dao = new ImagenesDAO(); imagenes obj = dao.obtener(id); Console.WriteLine("ayudaaaa"); return(obj); }
//funcion que busca una imagen por id de imagen public static imagenes FindImatgeById(int id) { imagenes imatge = (from c in contexto.imagenes where c.id == id select c).FirstOrDefault(); return(imatge); }
public ActionResult EliminarImagen(int id) { imagenes img = db.imagenes.Find(id); String ruta = Path.Combine(Server.MapPath("/Imagenes/"), img.nombre); System.IO.File.Delete(ruta); db.imagenes.Remove(img); db.SaveChanges(); return(RedirectToAction("Imagenes")); }
//funcion que añade un registro de una imagen subida ala base de datos public static void InsertImagen(String source, int id) { imagenes imagen = new imagenes(); imagen.src = source; imagen.id_usuario = id; contexto.imagenes.Add(imagen); contexto.SaveChanges(); }
public ActionResult NuevaImagen(imagenes imagen, HttpPostedFileBase archivo) { String Nombre = archivo.FileName; String path = Path.Combine(Server.MapPath("~/Imagenes"), Nombre); archivo.SaveAs(path); imagen.nombre = Nombre; db.imagenes.Add(imagen); db.SaveChanges(); return(RedirectToAction("Imagenes")); }
public ActionResult FileUpload(HttpPostedFileBase FileUpload1) { if (FileUpload1 != null) { var db = new muestraEntities1(); imagenes img = new imagenes(); using (BinaryReader reader = new BinaryReader(FileUpload1.InputStream)) { byte[] image = reader.ReadBytes(FileUpload1.ContentLength); img.imagen = image; img.active = true; db.imagenes.Add(img); db.SaveChanges(); } } return(RedirectToAction("Index", "Home")); }
//funcion que borra una imagen con un id en concreto. public static String DeleteImatge(int id) { String missatge = ""; imagenes imatge = FindImatgeById(id); try { contexto.imagenes.Remove(imatge); contexto.SaveChanges(); } catch (DbUpdateException ex) { SqlException sqlEx = (SqlException)ex.InnerException.InnerException; missatge = sqlEx.Message; contexto.imagenes.Add(imatge); } return(missatge); }
public ActionResult EditarNoticia(noticias noticia, HttpPostedFileBase imagen) { db.Entry(noticia).State = EntityState.Modified; db.SaveChanges(); var imagenes = db.imagenes.ToList(); int id = 0; foreach (var i in imagenes) { if (i.id_noticia == noticia.id_noticia) { id = i.id_imagen; } } imagenes img = new imagenes(); img = db.imagenes.Find(id); String ruta = Path.Combine(Server.MapPath("/Imagenes/"), img.nombre); System.IO.File.Delete(ruta); String Nombre = imagen.FileName; String path = Path.Combine(Server.MapPath("~/Imagenes"), Nombre); imagen.SaveAs(path); img.id_noticia = noticia.id_noticia; img.nombre = Nombre; img.id_imagen = id; db.Entry(img).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Noticias")); }
public ActionResult EditarImagen(imagenes imagen, HttpPostedFileBase archivo) { if (ModelState.IsValid) { var img = db.imagenes.Find(imagen.id_imagen); String ruta = Path.Combine(Server.MapPath("/Imagenes/"), img.nombre); System.IO.File.Delete(ruta); String Nombre = archivo.FileName; String path = Path.Combine(Server.MapPath("~/Imagenes"), Nombre); archivo.SaveAs(path); img.id_imagen = imagen.id_imagen; img.nombre = Nombre; img.id_noticia = imagen.id_noticia; db.Entry(img).State = EntityState.Modified; db.SaveChanges(); } return(RedirectToAction("Imagenes")); }
public ActionResult NuevaNoticia(noticias noticia, HttpPostedFileBase imagen) { db.noticias.Add(noticia); db.SaveChanges(); var max = db.noticias.Select(noticias => noticia.id_noticia).Max(); String Nombre = imagen.FileName; String path = Path.Combine(Server.MapPath("~/Imagenes"), Nombre); imagen.SaveAs(path); imagenes img = new imagenes(); img.nombre = Nombre; img.id_noticia = max; db.imagenes.Add(img); db.SaveChanges(); return(RedirectToAction("Noticias")); }
public ActionResult EditarImagen(int id) { imagenes imagen = db.imagenes.Find(id); return(View(imagen)); }