在asp.net mvc中使用foreach在一行中显示5列

时间:2014-09-27 10:57:16

标签: asp.net-mvc razor

我遇到了一件小事,我正在使用MVC5并尝试从数据库访问表并在View上显示它。 只有一列的表我这样做但不是一种有效的方法,所以需要你的帮助

@{
 int c = Model.Count();
}

   @foreach (var item in Model)
         {
             if (c >= 16)
             {
              <td>@Html.ActionLink( item.Name,"Jobs" ,new{name = item.Name})</td>   
             }
             else if (c== 15)
             {
                 <tr></tr>
              <td>@Html.ActionLink( item.Name,"Jobs" ,new{name = item.Name})</td>   
             }
             else if (c>=11)
             {
                 <td>@Html.ActionLink( item.Name,"Jobs" ,new{name = item.Name})</td>   
             }
             else if (c==10)
              {
                 <tr></tr>
              <td>@Html.ActionLink( item.Name,"Jobs" ,new{name = item.Name})</td>   
             }

             else if (c>=6)
             {
                 <td>@Html.ActionLink( item.Name,"Jobs" ,new{name = item.Name})</td>   
             }
             else if (c==5)
             {
                <tr></tr>
              <td>@Html.ActionLink( item.Name,"Jobs" ,new{name = item.Name})</td>   
             }

             else if (c>=0)
             {
                 <td>@Html.ActionLink( item.Name,"Jobs" ,new{name = item.Name})</td>   
             }
             c --;
         }

   </tr></Table>

连续5个colomns,然后创建包含五列的新行。

1 个答案:

答案 0 :(得分:0)

假设您的模型还有其他属性,例如Column2Column3等,那么这将为您提供5列:

<table class="table"  >
    <tr>
        @foreach (var item in Model)
        {
            <td>@Html.ActionLink(item.Category, "Jobs")</td>
            <td>@Model.Column2</td>
            <td>@Model.Column3</td>
            <td>@Model.Column4</td>
            <td>@Model.Column5</td>
        }

    </tr>
</table>