防止在嵌套的foreach循环中重复html元素

时间:2017-09-19 20:38:37

标签: c# asp.net-mvc razor

我有2个内部集合,即AirAirPricingInfo和CommonV420BookingTraveler。有关pricinginfo的第一个foreach是读取baseprice,EquivalentBasePrice,Taxes,第二个带有passengerdetails的foreach是读取firstname,lastname和dob。两个集合中只有 3:3 项目组合时,它应显示3个div " term-description" ,其中包含两个集合的详细信息,即AirAirPricingInfo和CommonV420BookingTraveler 。但它推迟了6个divs,请建议

@foreach(var priceinginfo in Model.SOAPEnvelope.SOAPBody.UniversalAirCreateReservationRsp.UniversalUniversalRecord.AirAirReservation.AirAirPricingInfo)
{
    foreach (var passengerdetails in Model.SOAPEnvelope.SOAPBody.UniversalAirCreateReservationRsp.UniversalUniversalRecord.CommonV420BookingTraveler)
    { 
        <dl class="term-description">
            <dt>First name:</dt>
            <dd>@passengerdetails.CommonV420BookingTravelerName.First</dd>
            <dt>Last name:</dt>
            <dd>@passengerdetails.CommonV420BookingTravelerName.Last</dd>
            <dt>Passenger Type:</dt>
            <dd>@passengerdetails.Gender</dd>
            <dt>Date of Birth</dt>
            <dd>@passengerdetails.DOB</dd>
            @*<dt>Booking/Cabin Class</dt>
                <dd>@pricing.AirBookingInfo</dd>
                <dt>Baggage Allowance</dt>
                <dd>@pricing.AirFareInfo.First().AirBaggageAllowance.AirMaxWeight.Value @pricing.AirFareInfo.First().AirBaggageAllowance.AirMaxWeight.Unit</dd>*@
            <dt style="width: 100%">
                <table class="table">
                    <tr class="warning" style="text-align: left; color: #000">
                        <td class="col-md-2">Fare</td>
                        <td class="col-md-2">Equivalent Fare</td>
                        <td class="col-md-5">Taxes</td>
                        <td>Total Fare</td>
                    </tr>
                    @*  *@
                </table>
                <tr class="warning" style="text-align: left; color: #000">
                    @*<td class="col-md-2">AED 1200.00</td>*@
                    <td class="col-md-2">@priceinginfo.BasePrice</td>
                    <td class="col-md-2">@priceinginfo.EquivalentBasePrice</td>
                     <td class="col-md-5">@priceinginfo.Taxes</td>
                     <td>@priceinginfo.TotalPrice</td>
                </tr>
            </dt>
            @*<dt style="width: 100%">@pricing.AirFareCalc</dt>*@
            @*@*<dt style="width: 100%">BLR EY X/AUH EY DXB Q BLRDXB25.00Q BLRDXB10.00 130.22ULPRRTEY/YS EY X/AUH EY BLR Q DXBBLR10.00 130.22ULPRRTEY/YS NUC305.44END ROE64.331</dt>*@
            @*<dt style="width: 100%">NON ENDO/ NONREF</dt>*@
        </dl>
    }
}

1 个答案:

答案 0 :(得分:0)

我不熟悉您的数据模型,但如果AirAirPricingInfo和CommonV420BookingTraveler之间存在任何关系,那么您可以在内循环中使用if块仅发出与正在处理的AirAirPricingInfo相关的CommonV420BookingTraveler。 外循环。

@foreach(var priceinginfo in Model.SOAPEnvelope.SOAPBody.UniversalAirCreateReservationRsp.UniversalUniversalRecord.AirAirReservation.AirAirPricingInfo)
{
    foreach (var passengerdetails in Model.SOAPEnvelope.SOAPBody.UniversalAirCreateReservationRsp.UniversalUniversalRecord.CommonV420BookingTraveler)
    {
    if(passengerdetails.AirAirPricingInfo == priceinginfo)
    {
      //continue with your logic
    }
    }
}