2016-08-29 74 views
0

我需要使用其中包含數據庫數據的模型填充我的文章ViewModel,但是我有一種方法需要將其分配給我的某個屬性使用數據模型用方法填充視圖模型以及

圖像列表是需要該方法的屬性。 爲文章列表中的每個項目調用該方法一次。

這裏是我的代碼:

 public ActionResult ArticleTypes(string at) 
     { 
      articleViewModel.Images = new List<ImageInfo>(); 
      var query = (from a in db.Articles 
         where a.SelectedArticleType == at 
         select new ArticlesViewModel 
         { 
          Id = a.Id, 
          Body = a.Body, 
          Headline = a.Headline, 
          PostedDate = a.PostedDate, 
          SelectedArticleType = a.SelectedArticleType, 
          UserName = a.UserName, 


         }).ToList(); 
      articleViewModel.Images = imageService.GetImagesForArticle(articlemodel.Id.ToString()); 


      return View(query); 
    } 

我也試圖把方法的LINQ內:

public ActionResult ArticleTypes(string at) 
    { 
     articleViewModel.Images = new List<ImageInfo>(); 
     var query = (from a in db.Articles 
        where a.SelectedArticleType == at 
        select new ArticlesViewModel 
        { 
         Id = a.Id, 
         Body = a.Body, 
         Headline = a.Headline, 
         PostedDate = a.PostedDate, 
         SelectedArticleType = a.SelectedArticleType, 
         UserName = a.UserName, 
         Images = imageService.GetImagesForArticle(a.Id.ToString()) 
        }).ToList(); 



     return View(query); 
} 

它拋出的異常: 'System.NotSupportedException' 類型的異常發生在EntityFramework.SqlServer.dll中,但未在用戶代碼中處理

附加信息:LINQ to Entities不能識別該方法「System.Collections.Generic.List`1 [New_MinecraftNews_Webiste_MVC.Models.ImageInfo] GetImagesForArticle

+0

要顯示的列表視圖中的圖像? –

+0

我還沒有很好地理解這個問題。你是否意味着每次創建ArticlesViewModel時都需要調用imageService.GetImagesForArticle(id)? –

+0

在一個視圖中,我想顯示圖像列表,例如文章的頁面和其他我想要的第一張圖片@Reddy –

回答

0

我加了一個foreach循環末insted的別的和它的作品:

public ActionResult ArticleTypes(string at) 
    { 
     articleViewModel.Images = new List<ImageInfo>(); 
     var modelList = (from a in db.Articles 
        where a.SelectedArticleType == at 
        select new ArticlesViewModel 
        { 
         Id = a.Id, 
         Body = a.Body, 
         Headline = a.Headline, 
         PostedDate = a.PostedDate, 
         SelectedArticleType = a.SelectedArticleType, 
         UserName = a.UserName 
        }).ToList(); 


     foreach (var model in modelList) 
     { 
      model.Images = imageService.GetImagesForArticle(model.Id.ToString()); 
     } 


     return View(modelList); 
} 
相關問題