문제

나는 파싱할 수 없는 이 코드에서 i를 증가시킵니다.아래 코드에서 나에게 무슨 문제가 있는 걸까요?

  @{        
            int i=0;
            }
@foreach (var item in Model.PortServiceTariffIncluded)
{


         <tr id="@("sp" + item.ServiceId)">
         <td>@item.ServiceName     <input type="hidden" name="QuotationDetailList[i].ServiceId" value="@item.ServiceId" /></td>
         <td>@item.ServiceCriteria</td>
         <td>@item.ItemBasis</td>
          <td>@Html.DropDownListFor(model => model.Currency, ViewBag.CurrencyDd as SelectList) <input type ="text" id="Amount@i" name="QuotationDetailList[i].Amount" /></td>
         <td><input type="hidden" value="@item.ServiceId" class="serviceslist" name="serviceslist" /><input type ="button" id="@item.ServiceId" name="btnremove" value="Remove" class="clsremove" /></td>
         </tr>
         i = i + 1;
} 
도움이 되었습니까?

해결책

대신 다음을 사용하십시오.

@foreach (var item in Model.PortServiceTariffIncluded.Select((value, i) => new { i, value })
{
     <tr id="@("sp" + item.value.ServiceId)">
         <td>@item.ServiceName <input type="hidden" name="QuotationDetailList[item.i].ServiceId" value="@item.value.ServiceId" /></td>
         <td>@item.value.ServiceCriteria</td>
         <td>@item.value.ItemBasis</td>
         <td>@Html.DropDownListFor(model => model.Currency, ViewBag.CurrencyDd as SelectList) <input type ="text" id="Amount@item.i" name="QuotationDetailList[item.i].Amount" /></td>
         <td><input type="hidden" value="@item.value.ServiceId" class="serviceslist" name="serviceslist" /><input type ="button" id="@item.value.ServiceId" name="btnremove" value="Remove" class="clsremove" /></td>
     </tr>
}
.

기본적으로 item 변수가 반복자 (item.i)와 실제 항목 (item.value)으로 구성된 객체로 만듭니다.

다른 팁

먼저, QuotationDetailList :

@{ var QuotationDetailList = Model.QuotationDetailList;}

그런 다음 for 여기 대신에 foreach :

@for (var ListIndex = 0; ListIndex < Model.PortServiceTariffIncluded.Count(); ListIndex++)

이제 변수와 인덱스를 사용하여 항목을 참조할 수 있습니다.

<td>@QuotationDetailList[ListIndex].ServiceName     <input type="hidden" name="@QuotationDetailList[ListIndex].ServiceId" value="@QuotationDetailList[ListIndex].ServiceId" /></td>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top