I have declared a variable like this
@{ int i = 1; }
Now, inside foreach loop i want to assign the value of i each time the loop is processed;
@foreach (var line in Model.Cart.Lines)
{
<input type="hidden" name="item_name_@i" value="@line.Product.ProductName" />
<input type="hidden" name="amount_@i" value="@line.Product.Price" />
<input type="hidden" name="quantity_@i" value="@line.Quantity" />
@i++;
}
but it isn’t working.
Any solution?
If you need access to the index, it makes more sense to use a normal
forloop:Alternatively, you could use a LINQ expression: