I found the answer by reading the comment at the end of: http://stackoverflow.com/questions/4466469/razor-no-overload-for-method-write-takes-0-arguments
My problem was that I was already in a code block so when I had @(i++) it was computing and outputting. Because I was in a code block the solution was to simply use i++; without the at sign or parentheses. Example:
@{int i = 0;}
@foreach (var item in Model) {
...HTML for table rows where I use the variable i...
i++;
}
nablaodel
Member
16 Points
4 Posts
MVC3 Razor: hide variable increment
Dec 08, 2011 04:57 PM|LINK
In a view I have something like:
@{int i = 0;}
@foreach (var item in Model) {
...HTML for table rows where I use the variable i...
@(i = i + 1)
}
The problem is that next to each row the value of i is output. How do I suppress that?
Thanks,
EWG
ignatandrei
All-Star
134521 Points
21576 Posts
Moderator
MVP
Re: MVC3 Razor: hide variable increment
Dec 08, 2011 07:40 PM|LINK
@foreach (var item in Model) {
...HTML for table rows where I use the variable i...
@{i++}
}
nablaodel
Member
16 Points
4 Posts
Re: MVC3 Razor: hide variable increment
Dec 08, 2011 08:28 PM|LINK
@{i++}
throws the error that it expected a ;
@{i++;}
throws CS1501: No overload for method 'Write' takes 0 arguments
Here's the full code:
<table>
<tr>
<th>
Name
</th>
<th>
Quantity
</th>
<th>
AuditTime
</th>
</tr>
@{int i = 0;}
@foreach (var item in Model) {
<tr>
<td>
@item.name
@Html.Hidden("StatList[" + i + "].name", @item.name)
</td>
<td>
@Html.TextBox("StatList[" + i + "].cases", Math.Round(item.sheets / 5000, 0), new { @class = "quantity" }) cases
@Html.TextBox("StatList[" + i + "].reems", Math.Round(item.sheets % 5000 / 500, 0), new { @class = "quantity" }) reems
</td>
<td>
@Html.TextBox("StatList[" + i + "].audittime", String.Format("{0:MM/dd/yyyy H:mm tt}", item.audittime).ToLower(), new { @class = "date" })
</td>
</tr>
@{i++;}
}
</table>
ignatandrei
All-Star
134521 Points
21576 Posts
Moderator
MVP
Re: MVC3 Razor: hide variable increment
Dec 08, 2011 08:52 PM|LINK
Ok, forgot about ;
Now it is ok?
nablaodel
Member
16 Points
4 Posts
Re: MVC3 Razor: hide variable increment
Dec 08, 2011 10:11 PM|LINK
No, please see my previous post where I also wrote what error it throws when I tried that.
nablaodel
Member
16 Points
4 Posts
Re: MVC3 Razor: hide variable increment
Dec 08, 2011 11:33 PM|LINK
I found the answer by reading the comment at the end of: http://stackoverflow.com/questions/4466469/razor-no-overload-for-method-write-takes-0-arguments
My problem was that I was already in a code block so when I had @(i++) it was computing and outputting. Because I was in a code block the solution was to simply use i++; without the at sign or parentheses. Example:
@{int i = 0;} @foreach (var item in Model) { ...HTML for table rows where I use the variable i... i++; }