How does MVC ViewData work?

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-06-03T13:47:16Z Indexed on 2010/06/03 13:54 UTC
Read the original article Hit count: 312

Filed under:
|

I am toying around with an MVC application just to learn the technology. I am mocking up a check register application that keeps track of a checking account, what has cleared the bank and what has not. I am using EF for my model and everything seems to be working correctly insofar as Edit, Create, etc. However, I have a couple of totals at the top. One to show the bank's balance and another to show the actual balance. It works most of the time but when I do Edit's it does not always accurately reflect the new total and I need it to update everytime a change is made (i.e when something clears the bank):

[Authorize(Roles = "admin, checkUser")]
public ActionResult Index()
{
    var resultSet = from myChecking in chk.tblCheckings
                    orderby myChecking.id descending
                    select myChecking;

    ViewData.Model = resultSet.Take(45).ToList();

    //for balances
    var actualBalance = from theChecking in chk.tblCheckings
                    select theChecking.deposit - theChecking.withdrawal;

    var bankBalance = from theChecking in chk.tblCheckings
                      where theChecking.cleared == true
                      select theChecking.deposit - theChecking.withdrawal;

    //get bank balance
    ViewData["bankBalance"] = bankBalance.Sum();


    //get actual balance
    ViewData["actualBalance"] = actualBalance.Sum();

    return View();
}

I am showing the actual and other balance in the Index view as follows:

<td colspan="9" style="text-align: center; font-size: 11pt; color: white;">
  <%= string.Format("Bank Balance: {0:c}", ViewData["bankBalance"]) %>  ------
  <%= string.Format("Actual Balance: {0:c}", ViewData["actualBalance"]) %>
</td>

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about asp.net-mvc