0

我希望这两种方法传递给一个视图:

 public IEnumerable<ProfitAndCostViewModel> getProfitSum()
        {
            var profBalance = db.Profits
   .Where(x => x.IdUser.UserId == WebSecurity.CurrentUserId)
   .GroupBy(x => x.IdUser.UserId)
   .Select(x => new ProfitAndCostViewModel { ProfitSum = x.Sum(y => y.Value) })
   .ToList();
            return profBalance;
        }

        public IEnumerable<ProfitAndCostViewModel> getCostSum()
        {
            var costBalance = db.Costs
   .Where(x => x.IdUser.UserId == WebSecurity.CurrentUserId)
   .GroupBy(x => x.IdUser.UserId)
   .Select(x => new ProfitAndCostViewModel { CostSum = x.Sum(y => y.Value) })
   .ToList();
            return costBalance;
        }

在我的 ActionResult 我有这个:

var pcv = new ProfitAndCostViewModel();
            pcv.ProfModel =getProfitSum();
            pcv.CostModel =getCostSum();

             return View(pcv);

在 ProfitAndCostViewModel 代码中是这样的:

public double ProfitSum { get; set; }
        public double CostSum { get; set; }
        public double FinalBalance { get; set; }
        public IEnumerable<ProfitAndCostViewModel> ProfModel { get; set; }
        public IEnumerable<ProfitAndCostViewModel> CostModel { get; set; }

这是错误:The model item passed into the dictionary is of type 'WHFM.ViewModels.ProfitAndCostViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1 [WHFM.ViewModels.ProfitAndCostViewModel]'。`

4

1 回答 1

3

看起来您的视图是强类型的IEnumerable<ProfitAndCostViewModel>

@model IEnumerable<ProfitAndCostViewModel>

但在这里你将一个ProfitAndCostViewModel实例传递给它:

var pcv = new ProfitAndCostViewModel();
pcv.ProfModel =getProfitSum();
pcv.CostModel =getCostSum();
return View(pcv);

因此,您应该修复输入视图的模型:

@model ProfitAndCostViewModel
于 2013-03-15T09:32:41.627 回答