[英]increment in asp.net mvc razor view
我想在ASP.net MVC Razor視圖的Sr.No.列內的以下代碼的第一列顯示增量值。 我正在關注,但無法正常工作。 顯示0 ++; 僅在該列中。 我在做什么錯。
@{int i = 0;}
<table class="table">
<tr>
<th>Sr No.</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.ModifiedDate)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
<span>@i++;</span>
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.ModifiedDate)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.CurrencyCode }) |
@Html.ActionLink("Details", "Details", new { id = item.CurrencyCode }) |
@Html.ActionLink("Delete", "Delete", new { id = item.CurrencyCode })
</td>
</tr>
}
用剃刀語法,您應該為C#邏輯狀態表創建C#范圍。 您可以嘗試以下代碼嗎?
<td>
<span>@(i++)</span>
</td>
這應該工作:
@{int i = 0;}
<table class="table">
<tr>
<th>Sr No.</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.ModifiedDate)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
<span>@(++i)</span>
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.ModifiedDate)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.CurrencyCode }) |
@Html.ActionLink("Details", "Details", new { id = item.CurrencyCode }) |
@Html.ActionLink("Delete", "Delete", new { id = item.CurrencyCode })
</td>
</tr>
}
您不能這樣使用。
<td>
<span>@i++;</span>
</td>
您需要像這樣編寫代碼:
<td>
<span>@i</span>
</td>
您需要在<tr>
標記后增加i
值。 我已經為您的代碼提供了解決方案。
@{
var i = 1; @*sr no will start from 1*@
}
<table class="table">
<tr>
<th>Sr No.</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.ModifiedDate)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
<span>@i</span>
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.ModifiedDate)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.CurrencyCode }) |
@Html.ActionLink("Details", "Details", new { id = item.CurrencyCode }) |
@Html.ActionLink("Delete", "Delete", new { id = item.CurrencyCode })
</td>
</tr>
i++;
}
希望它能正常工作。
根據@Stephen Muecke建議,您可以使用:
<span>@(i++)</span>
或者您可以這樣使用:
<span>@i</span>
i++;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.