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

时间:2022-05-02 22:33:15

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

在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>

Where model is an IEnumerable<SomeObject>:

其中model是IEnumerable

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.

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

3 个解决方案

#1


0  

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.

你可以通过使用span和css样式来改善这一点。

#2


0  

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 :)

#3


0  

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.

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

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.

加号是你只循环一次并且没有嵌套表(所以事情更可能排成一行)但减号是html的字符串操作。

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

应该使用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>

#1


0  

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.

你可以通过使用span和css样式来改善这一点。

#2


0  

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 :)

#3


0  

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.

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

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.

加号是你只循环一次并且没有嵌套表(所以事情更可能排成一行)但减号是html的字符串操作。

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

应该使用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>