Reputation: 1189
I have a strongly typed view where I am trying to display a model when an id
is passed to my Controller action method.
I am trying to construct a link to each of this models as well.
For the purpose before the beginning of my foreach
I have an i=0
, and then I am trying to increase it by 1 using i++
. The problem is that the the i++
part doesn't work -- I have always zero as an id
in my link. Why?
Can someone help with this?
@{
var i = 0;
foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Content)
</td>
<td>
@Html.ActionLink("Details", "Details", new { id = i })
i++;
</td>
</tr>
}
}
Upvotes: 0
Views: 26
Reputation: 24270
Razor does its best to distinguish code from markup/html/text, which isn't easy in general. And in this case, i++
is considered text.
The reason is that there are tags following the foreach {
part, causing the reading mode to switch to markup/html/text. In that mode only @...
, @{...
or }
are recognized as code, and everything else is treated as markup/html/text.
If this happens, just put @{ ... }
around your statements, like this:
<td>
@Html.ActionLink("Details", "Details", new { id = i })
@{
i++;
}
</td>
Upvotes: 2