ASP.Net Core 2.0 CONSULTING

1

I am starting to use ASP.Net Core 2.0 and in the middle of this process I am doing a mini inventory project, I already managed to make entries to the inventory, I did the controllers with the scaffolding, I have my class "inventory" and my class "sales", my concern is as follows: I do not know how to register a sale in my view Add Sale also modify in my database, the inventory table by subtracting the amount of that product that I sold, so that when you go to my inventory view and it is updated every time you sell a product.

I do not know how to modify the controllers that Visual Studio generates when doing scaffolding, so I come to you.

Basically, I have a sale object which I am dealing with, with its property "quantity" and I want that quantity to be subtracted from my object in "inventory" which also has a quantity. It is worth mentioning that at the time of making my sale I have a relationship with my inventory table, that is, for each sale I choose the product I want to sell, but I have not made the logic to discount that amount of inventory.

Excuse me for explaining, I'm trying 100% and I'm starting this, thank you very much

        [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("VentasId,FechaCompra,Cantidad,TotalVenta,ProductoId,ClienteId")] Ventas ventas)
    {
        if (ModelState.IsValid)
        {
            _context.Add(ventas);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        ViewData["ClienteId"] = new SelectList(_context.Cliente, "ClienteId", "Direccion", ventas.ClienteId);
        ViewData["ProductoId"] = new SelectList(_context.Producto, "ProductoId", "ReferenciaFabrica", ventas.ProductoId);
        return View(ventas);
    }

MY INVENTORY MODEL:

public class Inventario
{
    [Required]
    public int InventarioID { get; set; }

    [Required]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    public DateTime FechaInventario { get; set; }

    [Required]
    public int StockMinimo { get; set; }

    [Required]
    public int StockMaximo { get; set; }

    [Required]
    [StringLength(20, ErrorMessage = "{0} debe tener una longitud de {1} caracteres.")]
    public string Ubicacion { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    public DateTime FechaSalida { get; set; }

    public int ProductoId { get; set; }
    public Producto Producto { get; set; }

}

MY MODEL SALES:

public class Inventario
{
    [Required]
    public int InventarioID { get; set; }

    [Required]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    public DateTime FechaInventario { get; set; }

    [Required]
    public int StockMinimo { get; set; }

    [Required]
    public int StockMaximo { get; set; }

    [Required]
    [StringLength(20, ErrorMessage = "{0} debe tener una longitud de {1} caracteres.")]
    public string Ubicacion { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    public DateTime FechaSalida { get; set; }

    public int ProductoId { get; set; }
    public Producto Producto { get; set; }

}
    
asked by Tyg0th 06.03.2018 в 21:32
source

1 answer

0

you should get the inventory and update its quantity As an example, it would be something like that

    if (ModelState.IsValid)
    {
        _context.Add(ventas);

        var inventario = _context.Inventario.FirstOrDefault(p => p.ProductoId == ventas.ProductoId);

        if (inventario != null)
            inventario.cantidad -= ventas.cantidad;

        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }

the SaveChanges() is in charge of making the registration and the modification

    
answered by 06.03.2018 / 21:42
source