我在此代码中增加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;
}
答案 0 :(得分: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
组成)的对象。
答案 1 :(得分: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>