0

In the view code that follows I need to replace the "foreach" loop at line 243 with something that will return only once the Shipment Date,Carrier and Tracking number. Currently the code returns the Shipment Date,Carrier and Tracking number for each Spipment package. So if I have 3 packages each line returns 3 times the same info on each line. I want to have them displayed once per shipmentpackage.

I am trying to learn ASP and MVC by working on this already developed project. Please let me know if you need more info.

@model InSite.Mvc.Models.ViewModels.OrderHistoryDetailViewModel
@section TemplateCssContent
{
<link href="/Styles/template/ma-order-details.css" rel="stylesheet" type="text/css" />
<link href="/Styles/print.css" rel="stylesheet" type="text/css" media="print" />
}
<div class="midwrapper2">
<div class="contentArea2">
    <div class="mavd-wrapper">
        @{ Html.RenderAction("MyAccountLeftNav", "Shared"); }
        <div class="contentWrapper2">
            <div class="content2">
                <div class="clr lfl w100">
                    <h1>
                        Order Detail</h1>
                    <div class="return-btn">
                        <a class="btn btnStyleC btn-back-order" href="@Url.Action("OrderHistory", "Account")">
                            Back to Order List</a></div>
                </div>
                @if (Model.ErpError.Length > 0)
                {
                    <div class="clr lfl w100 error">
                        @Html.Raw(Model.ErpError)
                    </div>
                }
                else
                {
                    <div class="clr lfl w100">
                        <div class="order-number-date">
                            <table>
                                <tr>
                                    <th class="col-1">
                                        <h3>
                                            Order #:</h3>
                                    </th>
                                    <td class="col-2">
                                        <h3>@Model.CustomerOrder.OrderNumber</h3>
                                    </td>
                                </tr>
                                <tr>
                                    <th class="col-1">
                                        <h3>
                                            Order Date:</h3>
                                    </th>
                                    <td class="col-2">
                                        <h3>@Model.CustomerOrder.OrderDate.ToShortDateString()</h3>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        @if (Model.WSC_ShowPONumber || Model.WSC_ShowTermsCode)
                        {
                            <div class="order-number-date">
                                <table>
                                    @if (Model.WSC_ShowPONumber)
                                    { 
                                        <tr>
                                            <th class="col-1">
                                                <h3>
                                                    PO#:</h3>
                                            </th>
                                            <td class="col-2">
                                                <h3>@Model.CustomerOrder.CustomerPO</h3>
                                            </td>
                                        </tr>
                                    }
                                    @if (Model.WSC_ShowTermsCode)
                                    {
                                        <tr>
                                            <th class="col-1">
                                                <h3>
                                                    Terms:</h3>
                                            </th>
                                            <td class="col-2">
                                                <h3>@Model.CustomerOrder.TermsCode</h3>
                                            </td>
                                        </tr>
                                    }
                                </table>
                            </div>
                        }
                    </div>
                    <div class="clr lfl w100">
                        <div class="bill-ship">
                            <table>
                                <tr>
                                    <th>
                                        <h4>
                                            Billing Information</h4>
                                    </th>
                                </tr>
                                <tr>
                                    <td>
                                        @Model.CustomerOrder.BTDisplayName
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        @Html.Raw(Model.CustomerOrder.BTAddress1)
                                    </td>
                                </tr>
                                @if (!String.IsNullOrEmpty(Model.CustomerOrder.BTAddress2))
                                { 
                                    <tr>
                                        <td>
                                            @Html.Raw(Model.CustomerOrder.BTAddress2)
                                        </td>
                                    </tr>
                                }
                                <tr>
                                    <td>
                                        @Html.CityCommaStateZip(Model.CustomerOrder.BTCity, Model.CustomerOrder.BTState, Model.CustomerOrder.BTZip)
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        @Model.CustomerOrder.BTCountry
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        @Model.CustomerOrder.BTPhone1
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        @Model.CustomerOrder.BTEmail
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <div class="bill-ship">
                            <table>
                                <tr>
                                    <th>
                                        <h4>
                                            Shipping Information</h4>
                                    </th>
                                </tr>
                                <tr>
                                    <td>
                                        @Model.CustomerOrder.STDisplayName
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        @Html.Raw(Model.CustomerOrder.STAddress1)
                                    </td>
                                </tr>
                                @if (!String.IsNullOrEmpty(Model.CustomerOrder.STAddress2))
                                { 
                                    <tr>
                                        <td>
                                            @Html.Raw(Model.CustomerOrder.STAddress2)
                                        </td>
                                    </tr>
                                }
                                <tr>
                                    <td>
                                        @Html.CityCommaStateZip(Model.CustomerOrder.STCity, Model.CustomerOrder.STState, Model.CustomerOrder.STZip)
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        @Model.CustomerOrder.STCountry
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        @Model.CustomerOrder.STPhone1
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        @Model.CustomerOrder.STEmail
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    <div class="clr lfl w100">
                        <table class="info-tbl tbl-1">
                            <tr>
                                <th class="od-item">
                                    Item #
                                </th>
                                <th class="od-desc">
                                    Item Description
                                </th>
                                <th class="od-qty">
                                    Qty Ordered
                                </th>

                                @if (Model.CustomerOrder.ShipmentPackages.Count > 0)
                                    { 


                                    <th class="od-ship">
                                        Ship Date
                                    </th>
                                    <th class="od-pack">
                                        Carrier
                                    </th>

                                    <th class="od-track">
                                        Tracking #
                                    </th>
                                    }

                                <th class="od-unit">
                                    Unit Price
                                </th>
                                <th class="od-ext">
                                    Extended Price
                                </th>
                            </tr>



                            @foreach (var orderLine in Model.CustomerOrder.OrderLines)
                            { 
                                <tr>
                                    <td class="od-item">
                                        @if (orderLine.ProductRoute != null)
                                        {
                                            <a href="@Url.RouteUrl(null, orderLine.ProductRoute)">@orderLine.ERPNumber</a>
                                        }
                                        else
                                        {
                                            @orderLine.ERPNumber
                                        }
                                    </td>
                                    <td class="od-desc">
                                        @orderLine.ShortDescription
                                    </td>
                                    <td class="od-qty">
                                        @String.Format("{0:0}", orderLine.QtyOrdered)
                                    </td>




                                    @foreach (var shipmentPackage in Model.CustomerOrder.ShipmentPackages)
                                        {
                                    <td class="od-ship">
                                            @shipmentPackage.ShipmentDate.ToShortDateString()
                                        </td>
                                        <td class="od-pack">
                                            @shipmentPackage.Carrier
                                        </td>

                                        <td class="od-track">
                                            @if (String.IsNullOrEmpty(shipmentPackage.TrackingURL))
                                            {
                                                @shipmentPackage.TrackingNumber
                                            }
                                            else
                                            {
                                                <a target= "_blank" href="@shipmentPackage.TrackingURL" >
                                                    @shipmentPackage.TrackingNumber</a>   
                                            }
                                        </td>

                                     }
                                    <td class="od-unit">
                                        @orderLine.ActualPrice.ToCurrency()
                                    </td>
                                    <td class="od-ext">
                                        @orderLine.ExtendedActualPrice.ToCurrency()
                                    </td>
                                </tr>
                            }
                        </table>

                    </div>
                    <div class="clr lfl w100">
                        <table class="tbl-total">
                            <tr class="subtotal">
                                <th class="col-1">
                                    Subtotal
                                </th>
                                <th class="col-2">@Model.CustomerOrder.OrderSubTotal.ToCurrency()
                                </th>
                            </tr>
                            @if (Model.CustomerOrder.DollarOffOrder > 0)
                            { 
                                <tr>
                                    <th class="col-1">
                                        Order Discount
                                    </th>
                                    <th class="col-2">@Model.CustomerOrder.DollarOffOrder.ToCurrency()
                                    </th>
                                </tr>
                            }
                            <tr>
                                <th class="col-1">
                                    Shipping Amount
                                </th>
                                <th class="col-2">@Model.CustomerOrder.ShippingAndHandling.ToCurrency()
                                </th>
                            </tr>
                            @if (Model.CustomerOrder.DollarOffShipping > 0)
                            { 
                                <tr>
                                    <th class="col-1">
                                        Shipping Discount
                                    </th>
                                    <th class="col-2">@Model.CustomerOrder.DollarOffShipping.ToCurrency()
                                    </th>
                                </tr>
                            }
                            <tr>
                                <th class="col-1">
                                    Sales Tax
                                </th>
                                <th class="col-2">@Model.CustomerOrder.TotalTax.ToCurrency()
                                </th>
                            </tr>
                            <tr class="total">
                                <th class="col-1">
                                    Order Total
                                </th>
                                <th class="col-2">@Model.CustomerOrder.OrderGrandTotal.ToCurrency()
                                </th>
                            </tr>
                        </table>
                    </div>
                    <div class="clr lfl w100 order-btns">
  <a class="btn btnStyleB btn-print" href="javascript:window.print();">Print</a> <a class="btn btnStyleA btn-rma" href="@Url.RouteUrl(new { controller = "Account", action = "RequestRma", orderId = String.IsNullOrEmpty(Model.CustomerOrder.ERPOrderNumber) ? Model.CustomerOrder.OrderNumber : Model.CustomerOrder.ERPOrderNumber })">
                            Return Request (RMA)</a>
                    </div>
                }
            </div>
        </div>
    </div>
</div>
</div>
<div class="reverse-footer">
</div>

1 Answer 1

2

Replace the foreach loop block:

@foreach (var shipmentPackage in Model.CustomerOrder.ShipmentPackages)
{
   ...
}

with:

var shipmentPackage = Model.CustomerOrder.ShipmentPackages.FirstOrDefault();
if (null != shipmentPackage)
{
    ...
}

Hope that helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi David, Thank you for your quick response. When I did the change that you sugested I am getting an error: "Compiler Error Message: CS0103: The name 'shipmentPackage' does not exist in the current context" How can I resolve that?
Can you tell me what line the compiler is complaining about? NOTE: my code is missing a semicolon after FirstOrDefault()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.