【问题标题】:c# foreach Dynamically generated HTML tablec# foreach 动态生成的html表格
【发布时间】:2019-02-24 12:06:12
【问题描述】:

@{ int count = 0;}
@if (Model != null)
{
    <table style="width:100%" border="1">                        
    @foreach (var item in Model)
    {
       @if (count == 0)
       {
           <tr>
       }
       <td style="width:25%">
           <table>
               <tr><td>@Html.DisplayFor(model => item.Item_Title)</td></tr>
               <tr><td>@Html.DisplayFor(model => item.Item_Content)</td></tr>
               <tr><td>@Html.DisplayFor(model => item.Item_Author)</td></tr>
           </table>
       </td>

       @if (count == 3)
       {
           </tr>
       }

       @if (count == 3)
       { count = 0; }
       else (count != 3)
       { count++; }
    }
    </table>
}

正如标题所说,

我尝试使用 FOREACH 在 HTML 中动态生成表格。

但是出现了问题。

我的逻辑是动态生成一个4*N的表。

但是红框上的代码被程序识别为文本。

导致红框中的程序无法运行。

我该如何解决?


在发生后添加@

他检测到没有尽头

使以下内容变为文本

终于跳出来不存在了}

【问题讨论】:

  • 您是否尝试将 @ 符号放在 if 语句的前面?即@if(count ==3)
  • 您应该编辑您的问题并将代码发布到那里,而不是在图片中
  • 嗨,欢迎来到 Stackoverflow!请在此处发布您的代码,而不是在任何其他外部来源。对于您的问题- if(count) -> if (count) 之后缺少一个空格
  • @wazdev 我在 @ 之前添加了 if 但它导致最外面的 if (Model != null) not found}

标签: c# foreach tr


【解决方案1】:

好的,当我将 If 条件的输出放入 Html.Raw 时,这似乎可以编译:

@{ int count = 0;}
@if (Model != null)
{
    <table style="width:100%" border="1">                        
    @foreach (var item in Model)
    {
        if (count == 0)
        {
            @Html.Raw("<tr>")
        }

        <td style="width:25%">
           <table>
               <tr><td>@Html.DisplayFor(model => item.Item_Title)</td></tr>
               <tr><td>@Html.DisplayFor(model => item.Item_Content)</td></tr>
               <tr><td>@Html.DisplayFor(model => item.Item_Author)</td></tr>
           </table>
       </td>

       if (count == 3)
       {
           @Html.Raw("</tr>")
       }

       if (count == 3)
       { count = 0; }
       else if (count != 3)
       { count++; }
    }
</table>}

【讨论】:

  • 我在@前加了if但是导致最外面的if (Model != null) not found}我修改的代码已经更新到上面了。还是我的位置不对?
  • 我重做了很多答案。看看它是否适合你。
  • 谢谢~可以成功了。我也找到了另外一种方式使用@:也有同样的效果
【解决方案2】:

感谢大家解决问题。

修改后的方案如下

@{ int count = 0;}

@if(Model != null)
{
    <table style="width:100%" border="1">                        
    @foreach (var item in Model)
    {
        if(count == 0)
        {                                
            @:<tr>
        }
        <td style="width:25%">
            <table>
                <tr><td>@Html.DisplayFor(model => item.Item_Title)</td></tr>
                <tr><td>@Html.DisplayFor(model => item.Item_Content)</td></tr>
                <tr><td>@Html.DisplayFor(model => item.Item_Author)</td></tr>
            </table>
        </td>
        if(count == 3)
        {
            @:</tr>
        }

        if(count == 3)
        { count = 0; }
        else
        { count++; }
    }
    </table>
}
</td>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-30
    • 2010-10-11
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 2019-01-04
    • 2013-07-07
    • 1970-01-01
    相关资源
    最近更新 更多