2016-10-03 98 views
1

我想在ASP.net MVC Razor视图的Sr. No.列内的以下代码的第一列显示增量值。我正在做下面的事情,但它不工作。它显示0 ++;只在该栏中。我在做什么错。在asp.net mvc剃刀视图中增量

@{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> 
} 
+0

我++ ;,取出@ – Sherlock

+2

' @(I ++)'(你需要周边括号) –

+0

' @i 我++;' –

回答

2

在剃刀语法中,您应该为C#逻辑statemens创建C#范围。你可以尝试下面的代码;

<td> 
    <span>@(i++)</span> 
</td> 
1

这应该工作:

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

按@Stephen Muecke建议你可以使用:

<span>@(i++)</span> 

或者你可以用这样的:

<span>@i</span> 
i++; 
+0

这个' @(i ++)'有效,但是这个' @i i ++;' 不是 –