2014-02-13 45 views
0

我是新来MVC和HTML编码的一般。来自C#列表的HTML表格

我有一个SQL表[日程表]与列,日期,活动ID,充满数据的用户ID。

我有一个设置了Schedule Model的MVC项目。

在我的控制器:

public List<Schedule> ScheduleList { get; set; } 
//... 

//Fill the Schedule list with the data in the database. 
ScheduleList = context.Schedule.ToList(); 

在我的HTML视图我想创建一个填充沿着顶部的每个活动和每个日期左侧向下/第一列的表。然后,表中的每个单元格都具有包含数据和AcitviyID的每个用户。

任何帮助或建议表示赞赏。

+1

尝试[教程](http://www.asp.net/mvc/tutorials/mvc-5 /介绍/工具入门)。 :) – CodeCaster

回答

2

您需要为模型添加其他一些内容。填充列标题的活动列表以及行标题的日期列表。说实话,你需要的只是标题的每个活动的名称和ID。

所以,你会添加另一种模式是这样的:

public class ActivityHeader { 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

那么接下来你的模型将需要也有(我将离开你弄清楚如何填充它们): 公共列表标题{得到;组; } public List Dates {get;组; }

然后你的剃须刀页面上,你会铺陈表是这样的:

<table> 
    <thead> 
     <tr> 
      @foreach (var header in Model.Headers) { 
       <th data-id="@header.Id">@header.Name</th> 
      } 
     </tr> 
    </thead> 
    <tbody> 
     @foreach (var date in Model.Dates) { 
      <tr> 
       <td>@date.ToShortDateString()</td> 
       // Figure out how to match the date and Activity to the list of Schedules. 
      </tr> 
     } 
    </tbody> 
</table>