gpt4 book ai didi

ASP.NET MVC : How to transfer more than one object to View method?

转载 作者:行者123 更新时间:2023-12-02 08:13:15 25 4
gpt4 key购买 nike

我完成了 NerdDinner 教程,现在我正在玩一些项目。索引页显示所有即将举行的晚餐:

    public ActionResult Index()
{
var dinners = dinnerRepository.FindUpComingDinners().ToList();
return View(dinners);
}

在DinnerRepository类中,我有方法FindAllDinners,我想添加到上面所有晚餐的索引方法编号,如下所示:

    public ActionResult Index()
{
var dinners = dinnerRepository.FindUpComingDinners().ToList();
var numberOfAllDinners = dinnerRepository.FindAllDinners().Count();
return View(dinners, numberOfAllDinners);
}

当然,这是行不通的。由于我对 OOP 还很陌生,所以我需要这方面的帮助。

谢谢,

最佳答案

创建 View 模型:

public class DinnerViewModel
{
public List<Dinner> Dinners { get; set; }
public int NumberOfAllDinners { get; set; }
}

public ActionResult Index()
{
var dinners = dinnerRepository.FindUpComingDinners().ToList();
var numberOfAllDinners = dinnerRepository.FindAllDinners().Count();
return View(new DinnerViewModel { Dinners = dinners, NumberOfAllDinners = numberOfAllDinners } );
}

关于ASP.NET MVC : How to transfer more than one object to View method?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2484852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com