2014-11-04 71 views
-1

我想使用viewdata.model向查看發送查詢結果。但該模型需要Ienumerable類型的模型項目。使用viewdata.model發送數據庫查詢到一個視圖

private RScontextSansEDM db = new RScontextSansEDM(); 
ViewData.Model = (from a in db.TmembresansEDMdb 
        join b in db.dbTassociation on a.Idassociation equals b.Idassociation 
        where a.Idassociation == ListeMembre.Idassociation 
        select new { a }); 
return View(); 

執行時出現錯誤。

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery1[<>f__AnonymousType31[RSTestSansEdm.‌​Models.Tmembre]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[RSTestSansEdm.Models.Tmembre]' 

和模型:

namespace RSTestSansEdm.Models 
{ 
    public partial class Tmembre 
    { 

    [Key] 
    public int Idmembre { get; set; } 
    public string Nommembre { get; set; } 
    public string Prenommembre { get; set; } 
    public string Mailmembre { get; set; } 
    public string SRCImage { get; set; } 
    public int Idassociation { get; set; } 

    //public virtual IEnumerable<Tmembre> MembreAssociation { get; set; } 
    } 

和視圖:

@model IEnumerable<RSTestSansEdm.Models.Tmembre> 
@foreach (var item in Model) {   
    <div class="divmembre"> 
    <div class="dphoto"><img src="~/Content/@item.SRCImage"/></div> 

請幫助:)

+0

您如何在視圖中使用ViewData.Model?顯示該代碼。嘗試LINQ語句結尾處的.ToList()以強制執行查詢。我不懷疑這是一個問題。 – SBirthare 2014-11-04 09:04:27

+1

'code' @model IEnumerable David 2014-11-04 09:09:17

+0

感謝您的答覆:)錯誤:傳遞給字典的模型項類型爲'System.Data.Entity.Infrastructure.DbQuery'1 [<> f__AnonymousType3' 1 [RSTestSansEdm.Models.Tmembre]]',但該字典需要一個'System.Collections.Generic.IEnumerable'1 [RSTestSansEdm.Models.Tmembre]'類型的模型項。 – David 2014-11-04 09:11:31

回答

0

此行

select new { a }); 

正在創建一個匿名對象。將其更改爲

ViewData.Model = (from a in db.TmembresansEDMdb 
       join b in db.dbTassociation on a.Idassociation equals b.Idassociation 
       where a.Idassociation == ListeMembre.Idassociation 
       select a); 
+0

你有沒有打算標記這個或任何你接受的其他問題的答案?祝好運在未來獲得幫助! – 2015-05-17 23:46:40

相關問題