2

Ok so I basically have a survey wizard with 4 parts in it, I need to display 5 separate results into each, but a for loop as below, will obviously display all results and will therefore break the wizard, how can I loop through this in a for loop? Any help will be greatly appreciated, thanks

@foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Category.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Survey.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Title)
            </td>       
        </tr>
    }

2 Answers 2

2

Assuming your Model is some sort of IENumerable

@{ var list = Model.ToList();
for (int i = 0; i < list.Count; i++)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => list[i].Category.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => list[i].Survey.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => list[i].Title)
            </td>       
        </tr>
    }
 }
Sign up to request clarification or add additional context in comments.

6 Comments

I basically need to display: 0 to 5, then 5 to 10, then 11 - 15 then 16 - 20
@Mic, what do you mean 'between 0' ?
Sorry I mean as in 1 - 5, It is an ICollection
Ok, what happens if you try what I gave you above?
Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS0021: Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.IEnumerable<STRA.Models.Question>' Source Error:
|
1

Go it, just used an incrementer, seems to work just fine with the Model

 @{
        int counter = 0;
        foreach (var item in Model)
        {
            counter++;
            if (counter > 1 && counter < 6)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Category.Title)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Survey.Title)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Title)
                    </td>
                </tr>
            }
        }
    }

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.