public ActionResult SaveMassiveEdit(List <Producto> productosList) { foreach (var p in productosList) { bool isUpdate = false; var prod = db.Productoes.FirstOrDefault(_ => _.Id == p.Id); if (p.PrecioActual != prod.PrecioActual) { prod.PrecioActual = p.PrecioActual; var newPrice = new PrecioHistorico() { Created = DateTime.Now, FechaDesde = DateTime.Now, IdProducto = prod.Id, Precio = prod.PrecioActual, Id = Guid.NewGuid() }; db.PreciosHistoricos.Add(newPrice); isUpdate = true; } if (p.Description != prod.Description) { prod.Description = p.Description; isUpdate = true; } if (p.Unidad != prod.Unidad) { prod.Unidad = p.Unidad; isUpdate = true; } if (p.Estado != prod.Estado) { prod.Estado = p.Estado; isUpdate = true; } if (isUpdate) { db.Entry(prod).State = EntityState.Modified; prod.Modified = DateTime.Now; } } db.SaveChanges(); return(Json(new { redirectUrl = Url.Action("Index", "Productoes") })); }
public ActionResult Create([Bind(Include = "Id,Description,Estado,Unidad,Photo,TipoProductoId, PrecioActual,Created,Modified")] Producto producto, HttpPostedFileBase image) { if (ModelState.IsValid) { #region Photo if (image != null && image.ContentLength > 0 && image.ContentLength < 155000) { var content = new byte[image.ContentLength]; image.InputStream.Read(content, 0, image.ContentLength); int indexOfLastDot = image.FileName.LastIndexOf('.'); string extension = image.FileName.Substring(indexOfLastDot + 1, image.FileName.Length - indexOfLastDot - 1); if (extension == "jpg" || extension == "jpeg") { #region New Photo Random r = new Random(); string newPhoto = String.Format("{0}_{1}", producto.Description, r.Next(0, 99)); string newPath = System.IO.Path.Combine(producto.FullPath, String.Format("{0}.{1}", newPhoto, extension)); #endregion #region Delete Old Photo if (!String.IsNullOrEmpty(producto.Photo)) { string oldPhoto = System.IO.Path.Combine(producto.FullPath, producto.Photo); if (System.IO.File.Exists(oldPhoto)) { System.IO.File.Delete(oldPhoto); } } #endregion producto.Photo = String.Format("{0}.{1}", newPhoto, extension); image.SaveAs(newPath); } } #endregion producto.Description = producto.Description.Trim(); producto.Id = Guid.NewGuid(); producto.Created = DateTime.Now; db.Productoes.Add(producto); db.SaveChanges(); if (producto.PrecioActual > 0m) { var newPrice = new PrecioHistorico() { Created = DateTime.Now, FechaDesde = DateTime.Now, IdProducto = producto.Id, Precio = producto.PrecioActual, Id = Guid.NewGuid() }; db.PreciosHistoricos.Add(newPrice); db.SaveChanges(); } return(RedirectToAction("Index")); } ViewBag.TipoProductoId = new SelectList(db.TipoProductoes, "Id", "Description", producto.TipoProductoId); return(View(producto)); }