Reputation: 187
it will be difficult to explain my problem but I'll try to be the more explicit as possible. Also sorry for my english...
Actually, I have this array in my view.
The code which retrieve that it the following :
@foreach (var item in Model)
{
TimeSpan result = DateTime.Now - item.OrderDate;
if (result.Days < 31)
{
<tr border="1" bgcolor="@Odd">
<td> @Html.DisplayFor(modelItem => item.Username) </td>
<td> @Html.DisplayFor(modelItem => item.OrderId) </td>
<td>
<ul style="list-style-type:none; padding:0; margin:0">
@if (item.OrderDetails != null)
{
foreach (var o in item.OrderDetails)
{
if (o.Pack == null)
{
<li> @Html.DisplayFor(modelItem => o.Product.Name) </li>
}
else
{
<li> <text>Pack</text> @Html.DisplayFor(modelItem => o.Pack.Name) </li>
}
}
}
</ul>
</td>
......... other td.............
</tr>
} // 1116 = 36 months
else if (result.Days > 31 && result.Days <= 1116)
{
if (item.OrderDetails != null)
{
foreach (var a in item.OrderDetails)
{
if (a.Pack != null)
{
<tr border="1" bgcolor="@Odd">
<td> @Html.DisplayFor(modelItem => item.Username) </td>
<td> @Html.DisplayFor(modelItem => item.OrderId) </td>
<td>
<ul style="list-style-type:none; padding:0; margin:0">
@if (item.OrderDetails != null)
{
foreach (var o in item.OrderDetails)
{
if (o.Pack != null)
{
<li><text>Pack </text> @Html.DisplayFor(modelItem => o.Pack.Name)</li>
}
}
}
</ul>
</td>
......... other td.............
</table>
So, for you know, I have two kind of Product : Product and Pack. In my first IF condition (result.days < 31), I retrieve Pack and Product, like my order number 59 because the order date is less than 31 days. That's work find.
Now, for my second IF condition >31 & <1116, I want that retrieves me just my PACK. It works fine too, I just have two pack and my product are not display, that's great (don't pay attention on unit price and quantity columns, i've just forgotten to uncommented some line at the moment where I took the screenshot that's why there is a price and a quantity where it should have nothing.). But like you can see, it retrieves me the line two times for one order when I am in the second condition, cause I have two foreach one in the other one. How could I pass throught it, just have one ligne like my first condition ?
I don't know if am I clear... You maybe want to know that I work with two models here. One OrderModel, and one OrderDetailModel. The first one contains OrderId, OrderDate, Username, ClientId The second one contain OrderDetailId, OrderId, ProductId, UnitPrice(for product), PackId, UnitPricePack.
Thanks a lot, for your answer, tips, links, whatever...
Upvotes: 0
Views: 97
Reputation: 907
This is not needed:
foreach (var a in item.OrderDetails)
{
if (a.Pack != null)
{
If you have an item from 31 days old to 1116 days old, and it has products and no packs, you will still get a tr element, and the Product & Pack column will be empty.
If that is no good for you then make a bool var, maybe called havePack,
var havepack = false,
foreach (var a in item.OrderDetails)
{
if (a.Pack != null)
{
havePack = true;
}
}
if (havePack)
{ //now make your <tr> element here
Upvotes: 1