0

I have a foreach loop in my view that is not working correctly. My Model.AllSelectedQuestions is a type of List. So if Model.AllSelectedQuestions have 5 objects inside it will display 5 lines of . What I want to be able to do is to have a counter aswell that displays here is an example the boxes are the links.

enter image description here

@{
    int nr = 1;
    foreach (SelectedQuestionViewModel items in Model.AllSelectedQuestions)
    {
        <a href="#" class="box" id="Question_(@items.QuestionID)"></a>
        <p>@nr++</p>
    }              
  }

The problem is that I get this result: nr++ nr++ nr++ nr++ nr++ when i have 5 objects in my list. But I want to get 1 2 3 4 5. Like the display example.

Any kind of help is appreciated!

Thanks

3
  • 3
    What's wrong with your existing approach??? Commented Apr 24, 2012 at 9:23
  • 1
    you could try to wrap your anchor tag in an ordered list, it automatically generates the number sequence. Commented Apr 24, 2012 at 9:31
  • @Drew could give me an example Commented Apr 24, 2012 at 9:39

2 Answers 2

3

maybe I'm wrong, but have You checked that code ?

<p>@(nr++)</p>
Sign up to request clarification or add additional context in comments.

Comments

0

It's easier, i think to just use an ordered list to handle the sequence.

<ol>
foreach (SelectedQuestionViewModel items in Model.AllSelectedQuestions)
{
    <li><a href="#" class="box" id="Question_(@items.QuestionID)"></a></li>
}
</ol> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.