2014-01-21 55 views
0

访问每个小区我需要的输出是这样的:动态分配列标题行和C#

enter image description here

列标题必须动态地分配。 房间也是动态分配的。

我想通过获取房间名称和日期来访问每个单元格(标记为X)。

for example: 
    - Room 1, 1/2/2014 
    - Room 2, 1/3/2014 
    - Room 3, 1/2/2014 
    - Room 4, 1/4/2014 

到目前为止,我已将项目分配给C#列表,但尚未将它们作为列标题进行绑定。

此外,每个单元必须是可点击的。

+0

这是什么样的应用程序,MVC?如果是这样,你使用的视图引擎是什么?我假设你不想在Ajax请求中做所有这些客户端? – cobolstinks

+0

@cobolstinks一个Web应用程序。绝对不是客户端.. – ohhzumm

回答

0

那么,如果你使用剃须刀视图引擎,你可以做类似下面的事情。

在您的控制器是会呈现你的页面:

public ActionResult SomeAction(){ 
    var yourlist = SomeMethodThatReturnsYourList(); 
    return("yourViewName",yourList); 
} 

然后在你的看法是这样的:

@model List<yourObjectTypes> 
<table> 
    <tr> 
     /** empty 0,0 table cell **/ 
     <th></th> 

     /** renders the table headers today - today + 4 days) **/ 
     @for (var i = 0;i<5; i++){ 
     <th> 
     @DateTime.Now().AddDays(i).ToShortDateString(); 
     </th> 
     } 
    </tr> 

    /** main loop renders rest of the table **/ 
    @Model.foreach(m=>{ 
     <tr> 
      <td>m.RoomName</td> 
      /** loop over the 5 days per every room in the list and put an x where appropriate **/ 
      @for(var x = 0;x<5;x++){ 
      @if(m.ShortDateString == @DateTime.Now().AddDays(x).ToShortDateString()){ 
       <td>X</td> 
      } 
      else{ 
       <td></td> 
      } 
      } 
     </tr> 
    }); 

</table> 

我写道,在此编辑器,所以可能有语法问题。在面对剃须刀语法问题时,我总是参考这篇文章。上面的代码应该至少足以引导你朝着正确的方向前进。祝你好运。

Razor Cheat Sheet