6

I have the following code in which I would like to have an inner foreach loop, but the code doesn't compile with the addition of the inner loop. Any help would be greatly appreciated.

foreach (var answer in q.Answers)
{
    <input type="checkbox" name="AnswerDetails" value="@answer.AnswerText" data-answerid="@answer.Id">@answer.AnswerText<br />

    foreach (var research in @answer.ResearchSet)
    {
       @:<p>@research.Image</p>
    }
}
2
  • you are missing @ razor symbol at outer foreach loop Commented Dec 16, 2013 at 7:18
  • also, you did not close your input tag properly Commented Dec 16, 2013 at 7:26

1 Answer 1

5
@foreach (var answer in q.Answers)
{
    <input type="checkbox" name="AnswerDetails" value="@answer.AnswerText" data-answerid="@answer.Id" />@answer.AnswerText<br />

    foreach (var research in answer.ResearchSet)
    {
       <p>@research.Image</p>
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Sergey, I've tried your suggestion but it didn't work for me. Would you be able to take a look at the code sample below and tell me what I've done wrong?
else if (q.QuestionFormat == QuestionFormatType.multiChoiceMultiAnswer) { @:<form action="#" class="submissionForm" data-questionid="@q.Id" data-questionformat="@q.QuestionFormat"> @foreach (var answer in q.Answers) { <input type="checkbox" name="AnswerDetails" value="@answer.AnswerText" data-answerid="@answer.Id" />@answer.AnswerText<br /> foreach (var research in answer.ResearchSet) { <p>@research.Image</p> } } <button type="submit" class="btn btn-primary">Save Answer</button> @:</form>
I've got it working Sergey, thank you for your help.
@AndreLashley sorry Andre, was AFK. Glad you have solved problem! Btw consider to create form with Html.BeginForm(). Also you need @: if you are mixing code and plain text. If you have html tags, Razor is pretty clever to define where code ends and tag starts

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.