How does MVC ViewData work?

I use the MVC application only to learn technology. I make fun of the registration register application, which tracks the current account, what cleared the bank and what not. I am using EF for my model and everything seems to work correctly since Edit, Create, etc. However, I have a couple of totals at the top. One to show the balance of the bank, and the other to show the actual balance. It works most of the time, but when I do Edit, it does not always accurately reflect the new value, and I need to update it every time a change occurs (for example, 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 show the actual and other balance as an index 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>
+3
source share
1 answer

MVC... M.

, , . .

[Authorize(Roles = "admin, checkUser")]
public ActionResult Index()
{
    IndexModel model = new IndexModel();

    var resultSet = from myChecking in chk.tblCheckings
                    orderby myChecking.id descending
                    select myChecking;

    model.Transactions = resultSet.Take(45).ToList();

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

    model.BankBalance = bankBalance.Sum();


    //get actual balance
    var actualBalance = from theChecking in chk.tblCheckings
                    select theChecking.deposit - theChecking.withdrawal;

    model.ActualBalance = actualBalance.Sum();

    return View(model);
}

( , ):

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

, .

+7

Source: https://habr.com/ru/post/1748350/


All Articles