2014-02-27 1 views
0

나는이 코드에서 i를 증분하여 구문 분석 할 수 없습니다. 아래 코드에서 나는 무엇이 잘못 되었습니까? 그런 다음asp.net mvc 면도기는 카운터 증분을 전달할 수 없습니다.

@{ var QuotationDetailList = Model.QuotationDetailList;} 

foreach 대신 여기에 for을 사용합니다 :

@{   
      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="[email protected]" 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; 
} 

답변

1

(10) 대신에, 다음을 사용

@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="[email protected]" 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.i)과 실제 항목 (item.value)로 구성된 객체로 당신의 item 변수가 발생합니다.

0

첫째, 당신의 QuotationDetailList를 포함 할 변수를 설정합니다 이제 사용 항목을 참조 할 수 있습니다

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

변수 및 색인 :

관련 문제