Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6122737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:56:02+00:00 2026-05-23T15:56:02+00:00

It does not seem to like @{index++;} , I have tried @{int index++}, @(index++),

  • 0

It does not seem to like @{index++;} , I have tried
@{int index++}, @(index++), @(int index++;)

This code didn’t throw errors when used with MVC 2.
Here’s it’s giving me
ambiguity warnings about index.

@model CartTest.Models.Cart

@{
    ViewBag.Title = "Index";
}

<h2>Cart Index</h2>


<table width="80%" align="center">


<thead><tr>
<th align="center">Quantity</th>
<th align="left">Item</th>
<th align="right">Price</th>
<th align="right">Subtotal</th>
</tr></thead>

<tbody>


@{int index = 0;}

@foreach (var line in Model.Lines)
{
<tr>
@Html.Hidden("Lines.Index", index);
<td align="center">@Html.TextBox("Lines[" + index + "].Quantity",line.Quantity)</td>
<td align="left">@line.Product.Name</td>
<td align="right">@line.Product.Price</td>
<td align="right">@(line.Quantity * line.Product.Price)</td>
<td align="right">@Html.ActionLink("Remove", "RemoveItem", new { productId = line.Product.ProductID }, null)</td>

</tr>

@{index++;}    

}

</tbody>


<tfoot>


</tfoot>


</table>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:56:03+00:00Added an answer on May 23, 2026 at 3:56 pm

    Try like this:

    @{int index = 0;}
    @foreach (var line in Model.Lines)
    {
        <tr>
           ...
        </tr>
        index++;
    }
    

    Now that’s just to make the Razor compiler happy. It’s not a solution I recommend. The real solution I would recommend you is to use editor templates:

    <table width="80%" align="center">
        <thead>
        <tr>
            <th align="center">Quantity</th>
            <th align="left">Item</th>
            <th align="right">Price</th>
            <th align="right">Subtotal</th>
        </tr>
        </thead>
        <tbody>
            @Html.EditorFor(x => x.Lines)
        </tbody>
        <tfoot>
    
        </tfoot>
    </table>
    

    and inside the corresponding editor template of a Line (~/Views/Shared/EditorTemplates/LineViewModel.cshtml) which will be rendered for each element of the Line collection:

    @model LineViewModel
    <td align="center">
        @Html.TextBoxFor(x => x.Quantity)
    </td>
    <td align="left">
        @Html.DisplayFor(x => x.Product.Name)
    </td>
    <td align="right">
        @Html.DisplayFor(x => x.Product.Price)
    </td>
    <td align="right">
        @Html.DisplayFor(x => x.CalculatedTotalPrice) 
    </td>
    <td align="right">
        @Html.ActionLink("Remove", "RemoveItem", new { productId = Model.Product.ProductID }, null)
    </td>
    

    See, no more ugly loops, weakly typed helpers, dealing with some indexes, etc… Everything works by conventions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The standard logging code does not seem to work instantiating a logger with the
I have a value I am trying to index that looks like this: Test
It does not seem that SendGrid has a free account that one could use
It does not seem to be documented very much: hsh.merge(other_hash){|key, oldval, newval| block} →
dblink does not seem to work when I use a named connection to a
Imagick::queryFontMetrics does not seem to be working. When I use the metrics provided by
Maven 2 does not seem to consider my @Test and @Ignore annotations. How do
Serialized varibale does not seem to retain its state from classes that were extended.
Inherited properties and MetadataType does not seem to work with client side validation in
Does anyone know why .hide(normal) does not seem to be working in jQuery 1.4.2?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.