1

この 2 つのメソッドを 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 に答える