Doubt with Update in Asp.NET MVC

0

Sorry for the question, I'm learning Asp.NET with EF and I'm having trouble performing a specific Update.

I want to make a system where there are cash transactions between accounts. The structural part is ok, but the problem is this:

When I perform the first update, the system does the transfer normally, that is, both accounts have $ 100.00, I make the transaction $ 10.00, and one account is $ 90.00 and another with R $ 110.00.

In the second transfer of the same value, an account is $ 80 and another with $ 100.

    [HttpPost]
    public ActionResult TransferCurrency(Usuario usuario, ViewModelTransfer viewModel)
    {
        if (usuario.Id == 0)
        {
            return HttpNotFound();
        }
        if (viewModel.ForId == null)
        {
            return HttpNotFound();
        }


        var usuarioTransaction = _context.Usuario.Single(c => c.NConta == viewModel.ForId);
        usuarioTransaction.Currency = usuario.Currency + viewModel.Transfer;


        var usuarioInDb = _context.Usuario.Single(m => m.Id == usuario.Id);
        usuarioInDb.Currency = usuario.Currency - viewModel.Transfer;

        _context.SaveChanges();


        return RedirectToAction("Index");
    }

Does anyone know the solution?

I know it's pretty basic, but I'm not able to develop logic.

    
asked by anonymous 25.11.2018 / 23:28

1 answer

0

The problem here is not the first, second or third transfer ... The point is that in operations you are assigning to your _context.Usuario the value of Currency of object Usuario received as a parameter of your action and adding or subtracting the amount of Transfer from ViewModel ...

    var usuarioTransaction = _context.Usuario.Single(c => c.NConta == viewModel.ForId);
    usuarioTransaction.Currency += viewModel.Transfer;


    var usuarioInDb = _context.Usuario.Single(m => m.Id == usuario.Id);
    usuarioInDb.Currency -= viewModel.Transfer;

    _context.SaveChanges();
    
27.11.2018 / 11:14