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 7834765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:18:53+00:00 2026-06-02T13:18:53+00:00

I have nested foreach loops in my view, and am having problems with the

  • 0

I have nested foreach loops in my view, and am having problems with the radio buttons.

@foreach (var header in Model.surveyHeaders)
{
    <h1>@header.Name</h1>

foreach (var subHeader in Model.surveySubHeaders.Where(x => x.SurveryHeaderID == header.ID))
{
<h2>@subHeader.Name</h2>

    foreach (var question in Model.surveyQuestions.Where(x => x.SurveySubHeaderID == subHeader.ID))
    {
        @Html.RadioButtonFor(x => x.surveyResults.Where(y => y.SurveyQuestionID == question.ID).First().Value, 1);
        @Html.RadioButtonFor(x => x.surveyResults.Where(y => y.SurveyQuestionID == question.ID).First().Value, 2);
        @Html.RadioButtonFor(x => x.surveyResults.Where(y => y.SurveyQuestionID == question.ID).First().Value, 3);            
        @question.Question
    }
}

}

The radio button name is always ‘Value’, so the radio buttons are not grouped. What can I do to achieve the grouping that is desired?

  • 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-06-02T13:19:00+00:00Added an answer on June 2, 2026 at 1:19 pm

    Ah collections in MVC, the joy! In order to make sure all fields are named accordingly in order to be correctly model bound on post, you need to use for loops, that will set the indexes correctly.

    Before doing so, you’re going to have to tweak your model structure to save your headaches. You need to re-arrange your logic so that you have a hierarchical object model in which you can iterate more cleaner (this way we’re getting away from logic in the view too!)

    Your survey header class, can’t you put a list of subheaders on it? Then your subheader class, can you put the list of questions for that subheader? That way you can do:

    @for (var i = 0; i < Model.SurveyHeaders.Count; i++)
    {
        <h1>@Model.SurveyHeaders[i].Name</h1>
    
        for (var j = 0; j < Model.SurveyHeaders[i].SubHeaders.Count; j++)
        {
            <h2>@Model.SurveyHeaders[i].SubHeaders[j].Name</h2>
    
            for (var k = 0; k < Model.SurveyHeaders[i].SubHeaders[j].Questions.Count; k++)
            {
                @Html.RadioButtonFor(x => x.SurveyHeaders[i].SubHeaders[j].Questions[k].Value , 1);
                @Html.RadioButtonFor(x => x.SurveyHeaders[i].SubHeaders[j].Questions[k].Value , 2);
                @Html.RadioButtonFor(x => x.SurveyHeaders[i].SubHeaders[j].Questions[k].Value , 3);            
                @Model.SurveyHeaders[i].SubHeaders[j].Questions[k].Question
            }
        }
    }
    

    This assumes your new model structure is something like (with the following classes):

    public class MyModel
    {
        public List<SurveyHeader> SurveyHeaders { get; set; }
    }
    
    public class SurveyHeader
    {
        public string Name { get; set; }
        public List<SubHeader> SubHeaders { get; set; }
    }
    
    public class SubHeader
    {
        public string Name { get; set; }
        public List<Question> Questions { get; set; }
    }
    
    public class Question
    {
        public int Value { get; set; }
        public string Question { get; set; }
    }
    

    Also one other tip, for future reference, you use the following LINQ in your code:

    x => x.surveyResults.Where(y => y.SurveyQuestionID == question.ID).First().Value
    

    You can simplify it because First can actually take a lamba, like so (although you should use FirstOrDefault and null check it in future just for safety):

    x => x.surveyResults.First(y => y.SurveyQuestionID == question.ID).Value
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problems breaking out off these nested loops correctly. What the code is
I currently have many linq expressions nested within foreach loops ... kind of defeating
I have nested xsl:for loops: <xsl:for-each select=/Root/A> <xsl:for-each select=/Root/B> <!-- Code --> </xsl:for> </xsl:for>
I have a method with a nested foreach collection (iterate a set of objects
What if I have nested loops, and I want to break out of all
Today I coded a function that uses two nested foreach loops. After seeing, that
Possible Duplicate: Breaking out of a nested loop I have this code foreach (___)
If I have a nested foreach loop how do I do break the inner
I have some code that appends into some files in the nested for loops.
I am dealing with two rows of a table, each with nested foreach loops

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.