2014-02-27 23 views
0

我在这个代码中增加我,我无法解析。我在下面的代码中出了什么问题?asp.net mvc剃刀不能通过计数器增量

@{   
      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

使用以下,来代替:

@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变量是由一个迭代(item.i)和实际项目(item.value)的一个对象。

0

首先,我会设置一个变量包含您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>