场景:视图应该有所有利润的总和,所有成本的总和,余额=利润-成本
public ProfitAndCostViewModel getTotalBalance()
{
var totalProfit = db.Profits.Where(p=>p.IdUser.UserId == WebSecurity.CurrentUserId).Sum(p=>p.Value);
var totalCost = db.Costs.Where(c=>c.IdUser.UserId == WebSecurity.CurrentUserId).Sum(c=>c.Value);
var balance = totalProfit - totalCost ;
return new ProfitAndCostViewModel { FinalBalance = balance };
}
控制器:
public ActionResult Index()
{
var pcv = new ProfitAndCostViewModel();
pcv.ProfModel =getProfitSum();
pcv.CostModel =getCostSum();
pcv.TOTALBALANCE = getTotalBalance();
return View(pcv);
}
看法:
@model WHFM.ViewModels.ProfitAndCostViewModel
@Model.FinalBalance.FinalBalance