html - 当行中没有足够的项目时,使动态生成的表保持在表单中

标签 html asp.net-mvc razor view foreach

我正在使用 asp.net MVC。我在 View 中连续动态生成了包含 5 个项目的表。但是当模型没有足够的 5 个项目时我有一个问题,项目宽度将增加以填充如下所示的行:

| x | x | x | x | x | (when have >=5 item)

|    x    |    x    | (when have <5 item, in this case is 2)

| x | x |             (what I want to display) 

这是我的 cshtml 代码:

<table style="width:100%;height:100%">
    @{
        int count = 0;
        foreach (var thread in Model)
        {
            if (count % 5 == 0)
            {
                @:<tr>
            }
            <td style="width:20%">
                //thread details here
            </td>
            count++;
            if (count % 5 == 0)
            {
                @:</tr>
            }
        }
    }
</table>

我可以更改为每行 2 个项目或在 中使用固定宽度,但这不是我的目标。 你能建议我一些改进我的 table 的方法吗?

最佳答案

我通过添加更多空白解决了这个问题 <td>如果我的项目小于 5,则在行中。现在它显示为我想要的

<table style="width:100%;height:100%">
@{
    int count = 0;
    foreach (var thread in Model)
    {
        if (count % 5 == 0)
        {
            @:<tr>
        }
        <td style="width:20%">
            //thread details here
        </td>
        count++;
        if (count % 5 == 0)
        {
            @:</tr>
        }
    }
    while (count%5!=0)
    {
        <td></td>
        count++;
    }
    @:</tr>
}
</table>

关于html - 当行中没有足够的项目时,使动态生成的表保持在表单中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30388832/

相关文章:

python - 解析带有 <br> 标签的 html (Python)

javascript - 溢出 : Scroll on Y and Visible on X

html - Z-index 不按预期工作与图片和背景-div

javascript - 从 iframe 中抓取父页面 html

c# - 使用终结点路由时不支持使用 'UseMvc' 配置 MVC

asp.net-mvc - Microsoft 使用 ASP.NET 成员(member)资格提供商进行单元测试

javascript - 当 $(document).ready(

c# - 将 Asp.net MVC5 Razor 代码存储在数据库中并处理以在运行时获取值

asp.net-mvc - ASP.NET MVC 5中的级联下拉列表

asp.net-mvc - 防止静态文件处理程序拦截类似文件名的 URL