I am developing an application in MVC for Warehouse management, I have to update my inventory table, specifically the Stock field, when I receive the products I need to increase the Stock field and when I send products I need to discount the Stock field, I am only able to add a new row) and what I want is to update the row (Field Stock only) attached photo Table Inventario
My Controller [Post]:
//CREAMOS/DECLARAMOS LA TRANSACCION
using (var transaccion = db.Database.BeginTransaction())
{
try
{
//CARGO LOS DATOS A RECEPCION
Recepcion recepcion = new Recepcion
{
Kn_CodigoProveedor = proveeid,
Kn_CodigoBodega = bodeid,
UserId = usuaid,
f_Ingreso = fingreso,
f_Factura = ffactura,
f_Guia = fguia,
n_Guia = nguia,
n_Factura = nfactura,
n_OrdenCompra = nordencompra,
};
db.Recepcions.Add(recepcion);
db.SaveChanges();
//RECUPERO EL ULTIMO ID QUE GENERO (ULTIMA RECEPCION)
ultimarecepcionid = db.Recepcions.ToList().Select(r => r.Kn_CodigoRecepcion).Max();
//CICLO QUE GUARDA CADA ELEMENTO DEL DETALLE
foreach (ProductosRecepcion item in recepcionview.ProductosList)
{
var detalle = new RecepcionDetalle()
{
Kn_CodigoRecepcion = ultimarecepcionid,
Kn_CodigoProducto = item.Kn_CodigoProducto,
Foto = item.Foto,
d_Cantidad = item.d_Cantidad,
Precio_Unitario = item.Precio_Unitario,
};
//CARGO EL OBJETO AL DETALLE
db.RecepcionDetalles.Add(detalle);
foreach (ProductosRecepcion item1 in recepcionview.ProductosList)
{
//RECUPERO ULTIMO STOCK (TABLA INVENTARIO)
UltimoStock = db.Inventarios.Where(b => b.Kn_CodigoBodega == bodeid).Where(p => p.Kn_CodigoProducto == item.Kn_CodigoProducto).ToList().Select(p => p.Stock).Max();
}
//ARMO EL OBJETO PARA ACTULIZAR MI TABLA INVENTARIO
var stock = new Inventario()
{
Kn_CodigoProducto = item.Kn_CodigoProducto,
Kn_CodigoBodega = bodeid,
Stock = (detalle.d_Cantidad + UltimoStock),
};
//GUARDO
db.Inventarios.Add(stock);
}
db.SaveChanges();
//CONFIRMAMOS EXITO DE TRANSACCION
transaccion.Commit();
}
catch (Exception ex)
{
//CONFIRMAMOS FRACASO DE TRANSACCION
transaccion.Rollback();
ViewBag.Error = "ERROR: " + ex.Message;
return View(recepcionview);
}
}
Sorry for my english, any help for me?