Click here to Skip to main content
15,878,945 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

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

SQL
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


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


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???
Posted
Updated 10-Mar-17 8:55am
Comments
Karthik_Mahalingam 10-Mar-17 23:01pm    
apply debugger and see actually how you are getting the data

1 solution

It looks like that the rows coming from the controller side contains duplicates, you can eliminate duplicates by using the Distinct() method :

HTML
@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>
}
 
Share this answer
 
v3
Comments
Member 12839758 10-Mar-17 16:07pm    
making a Distic() statement only returns me 1 row foreach diferent "No_ped, but i wabnt the 3 rows from "No_Ped=200" but with their diferent lines, and now i get the amount of lines in return but every line gets the same values from the first one dunno why

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900