1

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

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 我有这个:

ProfitAndCostViewModel 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.IEnumerable`1[WHFM.ViewModels.ProfitAndCostViewModel]'.
4

2 回答 2

2

创建一个同时拥有它们的视图模型;

public class BigViewModel
{
    public List<ProfitsModel> ProfModel { get; set; }
    public List<CostsModel> CostModel { get; set; }
}

和控制器

public ActionResult Index()
{
    BigViewModel model =  new BigViewModel();
    model.costBalance = db.Costs...;
    model.profBalance = db.Profits...;

    return View(model)
}
于 2013-03-15T08:15:51.230 回答
1

您可以将参数包装到 ViewModel 或使用 ViewBag

于 2013-03-15T08:15:39.510 回答