2014-02-28 155 views
0

我已經將子類別表連接到我的模型中的主表。一種類似於下面的結構。通過控制器傳遞列表到IEnumerable視圖ASP.NET MVC 4

Structure of my sql

不過,我需要訪問不同表(子表)信息到我的主表,「文章」。所以我已經在索引的ArticleController中寫入了額外的代碼,如下所示。

CONTROLLER:

public ActionResult Index() 
    { 

     var articles = db.DataToArticles.Include(a => a.AgeGroup).Include(a => a.Disabilities).Include(a => a.StrategyType); 

     return View(articles.ToList()); 
    } 

「DataToArticles」 類似於上面的 「StrategyTypeToArticle」 表。

這裏是我的數據訪問層,我已經沒有任何問題與
DAL:

public class ArticleEntities : DbContext 
{ 
    public DbSet<AgeGroup> AgeGroups { get; set; } 
    public DbSet<Disabilities> Disabilitiess { get; set; } 
    public DbSet<StrategyType> StrategyTypes { get; set; } 
    public DbSet<Article> Articles { get; set; } 
    public DbSet<UserProfile> Profiles { get; set; } 
    public DbSet<DataToArticle> DataToArticles { get; set; } 


    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
     modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); 
    } 
} 

現在我的看法,這是給我的錯誤。
查看

@model IEnumerable<LearningEnterprises.Models.Article> 


的問題
問題正在通過文章變量在控制器我的看法IEnumberable。我知道它將它作爲List傳遞,但我沒有遇到這個問題。是否有解決方法?

回答

2

因爲您在查詢db.DataToArticles,因此您將IEnumerable<LearningEnterprises.Models.DataToArticle>傳遞給視圖,而不是IEnumerable<LearningEnterprises.Models.Article>

+0

謝謝,只是在我的模型和控制器做了一些修改。 –

0

我認爲你需要傳遞StrategyTypesTo然後你會得到相應的文章和VAR的文章可能不會文章類型不多,因爲你經過了這麼多的有

,以方便你可以使用動態或嘗試其他什麼建議

List<dynamic> articles = db.DataToArticles.Include(a => a.AgeGroup).Include(a => a.Disabilities).Include(a => a.StrategyType); 

@model List<dynamic> 
相關問題