2011-06-04 48 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