2011-06-04 49 views
0

我有2個表:LINQ查詢(數據資料)問題在.net MVC

  1. 分類(INT標識,串標題)
  2. 作品(INT的categoryId,串WorkTitle,INT ID)

Table Works有一個外鍵(多到單)到Categories表。

我想有這樣的結果:

Title (id 1) 

    WorkTitle (Works.id) | WorkTitle (Works.id) | WorkTitle (Works.id) (this is works in thsi category) 


    Title (id 2) 

    WorkTitle (Works.id) | WorkTitle (Works.id) | WorkTitle (Works.id) (this is works in thsi category) 

...... 

    Title (id n) 

    WorkTitle (Works.id) | WorkTitle (Works.id) | WorkTitle (Works.id)(this is works in thsi category) 

應該是什麼模型,視圖和控制器做到這一點的?

非常感謝!

+0

我不明白的問題...你想擁有上述數據的LINQ查詢?你有沒有嘗試過自己?顯然,該視圖只應顯示由控制器交付的數據(模型)。 – Rhapsody 2011-06-04 14:23:07

+0

嗨!問題是如何檢索數據並按照我寫的方式進行構建。我嘗試過,但我的解決方案是不可接受的。 – 2011-06-04 14:26:36

+0

你可以發佈你試過的東西,告訴我們爲什麼不接受?要檢索數據,我們需要知道數據的位置。也許EntityFramework(或類似)可以幫助你? – Rhapsody 2011-06-04 14:45:19

回答

1

這應該讓你開始。我首先假設Entity Framework 4.1代碼。使用NuGet中的ADO.NET Poco實體模板來填充數據庫初始化內容。

型號

public class Category 
{ 
    public virtual int Id { get; set; } 
    public virtual string Title { get; set; } 
    public virtual ICollection<Work> Works { get; set; } 
} 

public class Work 
{ 
    public virtual int Id { get; set; } 
    public virtual string Title { get; set; } 
} 

控制器

public ActionResult Details(int id) 
{ 
    Category c = context.Categories.Single(x=>x.Id == id); 
    return View(c); 
} 

查看

@model ICollection<Category> 
@{ 
    foreach(Category c in Model) 
    { 
     <h1>@c.Title</h1> (id @c.Id) 
     <ul> 
     foreach(Work w in c.Works) 
     { 
      <li>@w.Title (@w.Id)</li> 
     } 
     </ul> 
    } 
} 
+0

非常感謝!!它非常棒!:) – 2011-06-05 20:28:41