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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:12:21+00:00 2026-05-26T14:12:21+00:00

I have a database which has three tables: A, B, and C. I want

  • 0

I have a database which has three tables: A, B, and C.

I want the view to render the data in a particular way. Looping through A, then listing rows of B under A where B.aID = A.ID, listing each row of C under each row of B where C.bId = B.ID.

I’m new to ASP.NET MVC3 and I’m not sure how to accomplish this. All the examples I’ve seen are straight forward.

  • 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-26T14:12:22+00:00Added an answer on May 26, 2026 at 2:12 pm

    I would start with adding three models: A, B and C. In these models, A contains a List of B’s and B contains a List of C’s. For testing purposes, I would also add something to uniquely identify them, such as a Name. Example:

    public class A
    {
        public string Name { get; set; }
    
        public List<B> Bs { get; set; }
    }
    
    public class B
    {
        public string Name { get; set; }
    
        public List<C> Cs { get; set; }
    }
    
    public class C
    {
        public string Name { get; set; }
    }
    

    In my controller, I would add an action method that fills these models with data. For testing purposes, I’m using dummy data.

    public ActionResult List()
    {
        C c1 = new C() { Name = "C1" };
        C c2 = new C() { Name = "C2" };
        C c3 = new C() { Name = "C3" };
        C c4 = new C() { Name = "C4" };
    
        B b1 = new B() { Name = "B1", Cs = new List<C>() { c1, c3 } };
        B b2 = new B() { Name = "B2", Cs = new List<C>() { c2, c4 } };
        B b3 = new B() { Name = "B3", Cs = new List<C>() { c1, c2, c4 } };
        B b4 = new B() { Name = "B4", Cs = new List<C>() { c1, c2, c3, c4 } };
    
        A a1 = new A() { Name = "A1", Bs = new List<B>() { b1, b2 } };
        A a2 = new A() { Name = "A2", Bs = new List<B>() { b3, b4 } };
        A a3 = new A() { Name = "A3", Bs = new List<B>() { b1, b2, b3, b4 } };
    
        List<A> listOfAs = new List<A>() { a1, a2, a3 };
        return View(listOfAs);
    }
    

    And for the final piece, the view to display all of this. Simply using @foreach in a nested manner gives us what we need:

    @model IEnumerable<HelloMvc.Models.A>
    
    <h2>List of As</h2>
    
    <table>
        <tr>
            <th></th>
        </tr>
    
    @foreach (var a in Model) {
        <tr>
            <td>
                @a.Name<br />
                @foreach (var b in a.Bs) { 
                    @b.Name<br />
    
                    foreach (var c in b.Cs)
                    { 
                                @c.Name<br />
                    }
                }
    
            </td>
        </tr>
    }
    

    Note that the “HelloMvc” is my namespace. You probably need to change this to your own namespace.

    Everything put together, this results in a list of A’s with nested B’s and C’s:

    A1
    B1
    C1
    C3
    B2
    C2
    C4
    A2
    B3
    C1
    C2
    C4
    B4
    C1
    C2
    C3
    C4
    A3
    B1
    C1
    C3
    B2
    C2
    C4
    B3
    C1
    C2
    C4
    B4
    C1
    C2
    C3
    C4
    

    (You’ll have to imagine some styling.) Hope this example helps you.

    (sidenote)
    Funny how that third foreach doesn’t have a ‘@’ in front of it. It’s not a typo or an error, if I put it there, an exception is raised. Small bug in the Razor view engine, I suspect.

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

Sidebar

Related Questions

I have a test results database which has the following three tables along with
I have a mysql database which has grown to over 200 tables in it.
I have three tables in my database which are: messages topics comments Each of
I have three tables: computers, which has columns for id, serial number, make, model,
I have a database which has form authentication tables for an website [let say
I have a database which has a NOT NULL constraint on a field, and
I have to connect to a legacy postgres database which has ENCODING = 'SQL_ASCII';
I have a table (session) in a database which has almost 72,000 rows. I
I have a setting stored in database which has a value .jpg|.gif|.png . I
Problem: I have an address field from an Access database which has been converted

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.