Click here to Skip to main content
15,936,982 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Soo im trying to create a aplication where people can control his expenses and earnings, and what im trying to do now is based on a list of earnings and expenses get the respective balance for the User that is loged in

**my Balance model:**

C#
public class Balance
    {
        public Balance()
        {
            Expenses = new List<Expense>();
            Earnings = new List<Earning>();
        }

        public int BalanceId { get; set; }
        public List<Expense> Expenses { get; set; }
        public List<Earning> Earnings { get; set; }
        public string ApplicationUserId { get; set; }
    }
}


basicly evertyme a user add a expensive and a earning i dont want just to add the earning but i want to add to the list of earnings that is on my model Balance thats what i did:

**Controller Expense ActionMethod Create**

C#
public ActionResult Create([Bind(Include = "ExpenseId,TipeExpenseId,ExpenseDescription,ExpenseValor,PaymentTypeId,Data,Comentario")] Expense expense)
        {
            var userId = User.Identity.GetUserId();
            if (ModelState.IsValid)
            {

                var balance = db.Balance.Where(d => d.ApplicationUserId == userId).FirstOrDefault();


                expense.ApplicationUserId = userId;

                if (balance == null)
                {
                    Balance s = new Balance();
                    s.Expenses.Add(expense);
                    s.ApplicationUserId = userId;
                    db.Balance.Add(s);             
                }
                else
                {
                    balance.Expenses.Add(expense);
                }

                db.Expenses.Add(expense);
                db.SaveChanges();
                return RedirectToAction("Index");
            }


            return View(expense);
        }


then i have my partialView that renders the value of the balance based on all records of expenses and earnings

Razor
<p>Saldo: @getBalance()</p>
    
    @functions 
    {
        HtmlString getBalance()
        {
            decimal balance = 0;
            if(Model != null)  //did a firstOrDefault on the controller
            {
                foreach (var item in Model.Earnings)
                {
                    balance += item.ExpenseValue;
                }
                foreach (var item in Model.Earnings)
                {
                    balance -= item.Earnings;
                }
            }
            return new HtmlString(balance.ToString());
        }


So the main problem here is that the list of expenses is not saving in the balance database, and i went in the database records and there is no column for the lists, and that actually confused me, the question that i have is if im doing this the right way or not?
Or like i thaught, just creating a ViewModel with the 2 lists and get all the data there and compare to the User is a better approach?

Just need to know how can i insert data in my list of models, they work like a viewModel, but i have no idea about how to populate the lists the rest of the atributes is fine, im just having problems populating the lists need to know what i am doing wrong on my code

Ps: Sorry for my Bad English, but really want to improve :)

What I have tried:

in my code ahead is what i already tryed, i just need 1 of 2 solutions, need someone to explain me how to set the values to the list of expenses for example so i can calculate the balance, or another approach to do this
Posted
Updated 27-Mar-16 6:28am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900