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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:33:16+00:00 2026-06-16T03:33:16+00:00

I need to display some parent-child relation data in view. Following is my code:

  • 0

I need to display some parent-child relation data in view.
Following is my code:

@model PagedList.IPagedList<Test.Domain.TestTable> 
<table width="100%">
    <tr>
        <td>Name</td>
        <td>Parent</td>
    </tr>
    @foreach (var item in Model)
    {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ParentID)
        </td>
    </tr>
    }
    <tr>
        <td>
            Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
            -----------------
            -----------------
        </td>
    </tr>
</table>

Now I want to display the parent name instead of parent id, which has following structure in database:
ID Name ParentID
1  John   Null
2  Dean  1
3  Sam   1


To display parent name, I tried following code, but none of this works:

  • @Model.Where(screenParent => screenParent.ID == item.ParentID).Select(screenParent => new { screenParent.Name})
    It displays
    System.Linq.Enumerable+WhereSelectEnumerableIterator`2[Test.Domain.TestTable,<>f__AnonymousType5`1[System.String]]

Can you suggest why it display above line of code?
Also what can be done to achieve the parent name instead of parent id.
Thanks in advance.

  • 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-16T03:33:17+00:00Added an answer on June 16, 2026 at 3:33 am

    Making a couple of assumptions here.

    Do you have a property on your model for Parent instead or are you just pulling in ParentID? If you are pulling in just the ID i suggest changing your model/mapping so that you are pulling in the entire object reference by the FK.

    If you are already pulling in the entire Parent object, know that entity framework won’t automatically lazy load your Parent for you. You have to explicitly tell it to load it when you query TestTable.

    You can do this in 2 different ways.

    1. Make your Parent property Virtual. i.e.

      public virtual TestTable Parent { get; set; }

      Then when you try and access TestTable.Parent it will load from the database. This will allow you to access the name.

    2. Explicitly load Parent when you query test table i.e.

      myDBContext.TestTables.Include(“YourParentPropertyName”).Where(….);

    The end results in your view will look like this:

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

    Let me know if that helps.

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

Sidebar

Related Questions

I'm using a RadTreeView to display some data and I need to be able
By default parent::display($tpl); loads components/com_my_component/views/my_component/tmpl/default.php , but in some cases i need to load
I have some hierarchical data that I need to display in a series of
I use winforms MDI window and display child windows on it. I need to
I am new in MVC. I display the parent view details in two parts,
I've a website and I need to display some language name in the current
I'm creating a Trivia app, and need some help designing my model relationships. This
I need a simple info box to display some status output, that I would
I have the need to display a specific view on a separate screen, similar
I need to display some shapes (5 rectangles to be precise) on my caption

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.