//[MultipleButton(Name = "action", Argument = "NuevaVenta")] public ActionResult NuevaVenta(int?page = null, Guid?cartId = null) { page = (page ?? 1); var productospl = db.Productos .Include(p => p.Categoria).OrderBy(p => p.Descripcion).ToPagedList((int)page, 12); //var productos = productospl.ToList(); var productViewModels = new List <ProductoViewModel>(); foreach (var product in productospl) { var colorIds = db.Inventarios.Where(i => i.ProductoId == product.ProductoId); var colors = db.Colors.Where(c => colorIds.Any(ci => ci.ColorId == c.ColorId)).Select(c => new SelectListItem { Text = c.Nombre, Value = c.ColorId.ToString() }); product.ColorId = colors;// new SelectList(colors, "ColorId", "Nombre"); } ; var details = new List <DetallesVentaTMP>(); var cookie = Request.Cookies["your-cart-id"]; if (cookie != null) { cartId = Guid.Parse(cookie?.Value); details = db.DetallesVentaTMPs.Where(d => d.CartId == cartId.Value).ToList(); } else { cookie = new HttpCookie("your-cart-id"); } var saleViewModel = new AgregarProdViewModel { DetallesVentas2 = details, AgregarProdPagedListVM = productospl, }; if (cartId != null) { cookie.Value = cartId.ToString();// JsonConvert.SerializeObject(details.Select(d => new { DetallesVentaTmpId=d.DetallesVentaTmpId })); cookie.Expires = DateTime.Today.AddDays(1); HttpContext.Response.Cookies.Add(cookie); } ViewBag.LocalidadId = new SelectList(db.Localidads, "LocalidadId", "Name"); return(View(saleViewModel)); }
public ActionResult NuevaVenta(AgregarProdViewModel newProduct) { if (ModelState.IsValid) { var cookie = Request.Cookies["your-cart-id"]; var cartId = cookie != null?Guid.Parse(cookie?.Value) : Guid.NewGuid(); var product = db.Productos.Find(newProduct.ProductoId); var tmp = db.DetallesVentaTMPs.FirstOrDefault(sdt => sdt.ProductoId == newProduct.ProductoId && sdt.ColorId == newProduct.ColorId && sdt.CartId == cartId); if (tmp == null) { tmp = new DetallesVentaTMP() { CartId = cartId, Descripcion = product.Descripcion, Precio = newProduct.Price, ProductoId = newProduct.ProductoId, Cantidad = newProduct.Cantidad, ColorId = newProduct.ColorId, }; db.DetallesVentaTMPs.Add(tmp); } else { tmp.Cantidad += newProduct.Cantidad; db.Entry(tmp).State = EntityState.Modified; } db.SaveChanges(); return(RedirectToAction("NuevaVenta", new { cartId = cartId })); } return(View(newProduct)); }