Foreach循环ASP MVC视图中的错误

时间:2013-05-08 16:18:35

标签: asp.net-mvc-3 foreach

在后面的视图代码中,我需要将第243行的“foreach”循环替换为只返回一次发货日期,运营商和跟踪号码的内容。目前,该代码返回每个Spipment包的发货日期,运营商和跟踪号。因此,如果我有3个包,每行返回每行相同信息的3倍。 我希望每个出货包装一次显示它们。

我正在尝试通过处理这个已经开发的项目来学习ASP和MVC。 如果您需要更多信息,请与我们联系。

@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 个答案:

答案 0 :(得分:2)

替换foreach循环块:

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

使用:

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

希望有所帮助。