如何在 MVC 中的单个 foreach 循环中创建水平表?

发布于 2024-09-04 20:43:14 字数 1297 浏览 7 评论 0原文

在 ASP.Net MVC 中,有什么方法可以将以下代码压缩为单个 foreach 循环吗?

<table class="table">
    <tr>
        <td>
            Name
        </td>    
        <% 
        foreach (var item in Model) 
        {
         %>
            <td>
                <%= item.Name %>
            </td>
        <% 
        } 
        %>
    </tr>
    <tr>
        <td>
            Item
        </td>    
        <% 
        foreach (var item in Model) 
        {
         %>
            <td>
                <%= item.Company %>
            </td>
        <% 
        } 
        %>
    </tr>
</table>

其中模型是 IEnumerable

public class SomeObject
{
   public virtual Name {get;set;}
   public virtual Company {get;set;}
}

这将输出一个表,如下所示:

Name     |    Bob    |     Sam    |    Bill   |     Steve    |
Company  |  Builder  |   Fireman  |     MS    |     Apple    |

我知道我可能可以使用扩展方法来写出每一行,但是是否可以使用单个方法构建所有行模型迭代?

这是这个问题的后续内容 因为我对我接受的答案不满意,并且不敢相信我已经提供了最好的解决方案。

Is there any way, in ASP.Net MVC, to condense the following code to a single foreach loop?

<table class="table">
    <tr>
        <td>
            Name
        </td>    
        <% 
        foreach (var item in Model) 
        {
         %>
            <td>
                <%= item.Name %>
            </td>
        <% 
        } 
        %>
    </tr>
    <tr>
        <td>
            Item
        </td>    
        <% 
        foreach (var item in Model) 
        {
         %>
            <td>
                <%= item.Company %>
            </td>
        <% 
        } 
        %>
    </tr>
</table>

Where model is an IEnumerable<SomeObject>:

public class SomeObject
{
   public virtual Name {get;set;}
   public virtual Company {get;set;}
}

This would output a table as follows:

Name     |    Bob    |     Sam    |    Bill   |     Steve    |
Company  |  Builder  |   Fireman  |     MS    |     Apple    |

I know I could probably use an extension method to write out each row, but is it possible to build all rows using a single iteration over the model?

This is a follow on from this question as I'm unhappy with my accepted answer and cannot believe I've provided the best solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终难愈 2024-09-11 20:43:14

如果您不限于使用纯表,那么这对您有用。

<table class="table"> 
    <tr> 
        <th> 
             Name<br/>
             Item<br/>
        </th>     
        <%  
        foreach (var item in Model)  
        { 
         %> 
            <td> 
                  <%= Html.Encode(item.Name) %><br/>
                  <%= Html.Encode(item.company) %><br/>
            </td> 
        <%  
        }  
        %> 
    </tr> 
</table> 

您绝对可以通过使用 span 和 css 样式来改进这一点。

If you are not restricted to using pure tables, then this would work for you.

<table class="table"> 
    <tr> 
        <th> 
             Name<br/>
             Item<br/>
        </th>     
        <%  
        foreach (var item in Model)  
        { 
         %> 
            <td> 
                  <%= Html.Encode(item.Name) %><br/>
                  <%= Html.Encode(item.company) %><br/>
            </td> 
        <%  
        }  
        %> 
    </tr> 
</table> 

You can definitely improve this by using span and css stylings.

执手闯天涯 2024-09-11 20:43:14

检查以下垫子它可以帮助您

<table class="table">
    <tr>
        <td>
            <table>
                <tr><td>Name</td></tr>
                <tr><td>Item</td></tr>
             <table>
        </td>    
        <% 
        foreach (var item in Model) 
        {
         %>
            <td>
                <table>
                  <tr><td><%= item.Name %></td></tr>
                  <tr><td><%= item.company %></td></tr>
                <table>
            </td>
        <% 
        } 
        %>
    </tr>
</table>

注意:-

No check though :)

Check the following mat it help you

<table class="table">
    <tr>
        <td>
            <table>
                <tr><td>Name</td></tr>
                <tr><td>Item</td></tr>
             <table>
        </td>    
        <% 
        foreach (var item in Model) 
        {
         %>
            <td>
                <table>
                  <tr><td><%= item.Name %></td></tr>
                  <tr><td><%= item.company %></td></tr>
                <table>
            </td>
        <% 
        } 
        %>
    </tr>
</table>

Note:-

No check though :)
东走西顾 2024-09-11 20:43:14

非常旧的线程,但是...

我使用了以下内容(但是当我遇到这个问题时正在寻找更好的方法)

创建一个循环遍历对象并为变量中的每一行创建 HTML 的代码块,然后输出变量。

优点是你只循环一次并且没有嵌套表(所以事情更有可能排列),但缺点是 html 的字符串操作。

可能应该使用 stringbuilder 而不是 string,但原理是相同的。

 @code
        Dim tr1 As String = "<th>Name</th>"
        Dim tr2 As String = "<th>Company</th>"

        For Each thingy As object In Model

            tr1 += "<th>" & thingy.Name & "</th>"
            tr2 += "<th>" & thingy.Company & "</th>"

        Next



        End Code

        @<table class="table table-condensed">
            <tr>@Html.Raw(tr1)</tr>
            <tr>@Html.Raw(tr2)</tr>
         </table>

Very old thread, but...

I've used the following (but was looking for a better approach when I came across this question)

Create a code block that loops through the objects and creates the HTML for each row in a variable, then output the variable.

The plus is you're only looping once and do not have nested tables (so things are more likely to line up) but the minus is the string manipulation of html.

Should probably use stringbuilder rather than string, but principle is the same.

 @code
        Dim tr1 As String = "<th>Name</th>"
        Dim tr2 As String = "<th>Company</th>"

        For Each thingy As object In Model

            tr1 += "<th>" & thingy.Name & "</th>"
            tr2 += "<th>" & thingy.Company & "</th>"

        Next



        End Code

        @<table class="table table-condensed">
            <tr>@Html.Raw(tr1)</tr>
            <tr>@Html.Raw(tr2)</tr>
         </table>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文