Html表foreach给出相同的结果 [英] Html table foreach giving same results
本文介绍了Html表foreach给出相同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个HTML,我从模型填充但不是给我模型中的每一列我得到基于Id的重复行
这个是我的表Squema和我的愿望输出:
I have a HTML wich i fill from a Model but instead of giving me every column in my Model im getting repeated rows based on the "Id"
This is my table Squema and my desire Output:
ID | LINE | ART | QUANTITY | PRICE | TOTAL 200 1 5 3 10 30
200 2 7 5 100 500
200 3 7 2 100 200
201 1 9 1 40 40
201 2 12 3 35 105
然而我的桌子就像这样填充:
However my table gets filled like this:
ID | LINE | ART | QUANTITY | PRICE | TOTAL 200 1 5 3 10 30
200 1 5 3 10 30
200 1 5 3 10 30
201 1 9 1 40 40
201 1 9 1 40 40
正如你所看到的,我总是得到第一个行结果,但重复了行数,而不是每个不同的线由相同的Id
我尝试过:
这是我的表填充
As you can see i always get the first "Line" result but repeated by the number of lines, instead of each diferent line by the same "Id"
What I have tried:
This is My Table Filler
<pre><tr id="details" style="display:none;">
<td colspan="6">
<table>
<tr class="table-tr-header">
<td align="center" class=""># Ped</td>
<td align="center" class="">Line</td>
<td align="center" class="">Art</td>
<td align="center" class="">Quantity</td>
<td align="center" class="">Price</td>
<td align="center" class="">Total</td>
<td><input type="checkbox" name="myTextEditBox" value="checked" /></td>
</tr>
@foreach (var itemChild in Model.compdl))
{
<tr>
<td align="center">@itemChild.Id</td>
<td align="center">@itemChild.Line</td>
<td align="center">@itemChild.Art</td>
<td align="center">@itemChild.Quantity</td>
<td align="center">@itemChild.Price</td>
<td align="center">@itemChild.Total</td>
</tr>
}
</table>
</td>
</tr>
我的Compdl模型是:
My Compdl Model is :
public partial class Compdl
{
public long Id { get; set; }
public long Line { get; set; }
public int Art { get; set; }
public int Quantity { get; set; }
public decimal Price{ get; set; }
public double Total { get; set; }
}
我做错了什么???
What am i doing wrong???
推荐答案
看起来来自控制器端的行包含重复项,您可以使用Distinct()
方法消除重复项:
It looks like that the rows coming from the controller side contains duplicates, you can eliminate duplicates by using theDistinct()
method :
@foreach (var itemChild in Model.compdl.Distinct())
{
<tr>
<td align="center">@itemChild.Id</td>
<td align="center">@itemChild.Line</td>
<td align="center">@itemChild.Art</td>
<td align="center">@itemChild.Quantity</td>
<td align="center">@itemChild.Price</td>
<td align="center">@itemChild.Total</td>
</tr>
}
这篇关于Html表foreach给出相同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文