Doubt with Asp.NET MVC Update

Asked

Viewed 61 times

0

Sorry for the question, I am learning Asp.NET with EF and am having problems 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 the following:

When I perform the first update, the system makes the transfer normally, IE, both accounts have R $ 100,00, I make the transaction of R $ 10,00, and one account has R $ 90,00 and another with R $ 110,00.

In the second transfer of the same amount, one account is R $ 80 and another with R $ 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");
    }

Someone knows the solution?

I know it’s pretty basic, but I’m not getting the logic.

  • 1

    Take a look at the assignment, there’s the problem! picking up wrong values

1 answer

0


The problem here is not the first, second or third transfer... The issue is that in the operations you are assigning to your _context.Usuario the value of Currency of the object Usuario received as a parameter of your action and adding or replacing the amount of Transfer of 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();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.