我試圖找出填充嵌套集合模型的好方法。我在我的視圖模型中有以下內容。在視圖模型中填充嵌套的ICollection
public class ListViewModel
{
public ICollection<Wish> Wishes { get; set; }
}
希望模型是這樣的:
public class Wish
{
public ICollection<Image> Images { get; set; }
}
現在在我的控制器,我想填充ListViewModel有意願也填充每個希望與它們對應的圖像。我有什麼至今:
public IActionResult Index()
{
ICollection wishes = _repoWish.GetAllAsync().Result;
ICollection images = _repoImage.GetAllAsync().Result;
var model = new ListViewModel
{
Wishes = wishes
};
return View(model);
}
我知道我可以做很多的foreach語句,但我想利用LINQ的填充與它們對應的圖像中的每個願望。
**我確實有一個通用的存儲庫類,它可以讓我以與願望相同的方式檢索所有圖像。
***將存儲庫視爲上下文。 所以不是_repoWish和_repoImage其wishContext和imageContext
我使用ASP.NET 2.0的核心與實體框架核心
'_repoWish.GetAllAsync()。'返回什麼?以及如何在結果集中連接Image實體和Wish實體? – Shyju
所以你的願望不是來自你的數據庫中的圖像? – Zinov
如果您[使用視圖模型],您需要循環或'linq'(https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc)。你現在的代碼有什麼問題? – adiga