Return amount Asp.net MVC

Asked

Viewed 56 times

0

I have a model with category, Deposit, Date, Amount, I need the Amount add all the deposit that exist in the database and returns in the View, I did the following, only it didn’t solve.

public async Task<ViewResult> Amount()
{
    var movements = _context.Movement.Include(x => x.Deposit).ToList();

    var total = movements.Sum(x => x.Amount);
    ViewData["amount"] = total;
    return View(total);
}

I am passing the following parameter in View:

@ViewData["amount"]
  • Add your View code so we know how you are trying to access this information.

  • @Tallessantana Hello, I modified.

  • 1

    You are passing the amount twice to the view. Both in her Model and in Viewdata. Viewdata I think you need to cast so arrobaViewData["amount"] as Int32 can work. Another option is not to use Viewdata but the model. To do this, in the first line of your view add int arrobamodel Where you want to use the value use arrobamodel or arrobamodel

1 answer

0


Just add a + before the = for him to make the increment.

var total += movements.Sum(x => x.Amount);

Browser other questions tagged

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