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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:43:15+00:00 2026-05-24T23:43:15+00:00

I have the following models: public class Page { public int PageID { get;

  • 0

I have the following models:

public class Page
    {
        public int PageID { get; set; }
        public string Name { get; set; }
        public string Content { get; set; }
        public DateTime? DateCreated { get; set; }
        public bool IsPublished { get; set; }
        public string ModifiedBy { get; set; }
        public DateTime? DateModified { get; set; }

        public int UserID { get; set; }
        public int CategoryID { get; set; }

        public virtual User User { get; set; }
        public virtual Category Category { get; set; }
    }

public class Category
    {
        public int CategoryID { get; set; }

        [Required(ErrorMessage = "Category name is required.")]
        [Display(Name = "Category Name")]
        public string Name { get; set; }

        public virtual ICollection<Page> Pages { get; set; }

    }

and I want to populate this navigation list:

<div id="centeredmenu" class="nav-border nav-color">
    <ul>
    @foreach (var pages in Model)
    {
        <li>CATEGORY NAME GOES HERE
            <ul>
               @foreach (var pages in Model)
               {
                <li>PAGE NAMES GO HERE</li>
                }
            </ul>
        </li>
    }
    </ul>
</div>

but I’m having problems implementing the controller. I tried this ViewModel:

public class MainPageModels
    {
        public Category Categories { get; set; }
        public Page Pages { get; set; }
    }

but it just confused me even more with this error message:

System.Data.Edm.EdmEntityType: : EntityType ‘MainPageModels’ has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �MainModels� is based on type �MainPageModels� that has no keys defined.

This is my controller:

public ActionResult Index()
        {
            var pages = db.MainModels.Select(p => p.Pages).Select(c => c.Category);
            return View(pages);
        }

I may be missing something simple here.

  • 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-24T23:43:16+00:00Added an answer on May 24, 2026 at 11:43 pm

    Here my solution to my parent-child list problem:

    I created a ViewModel to house both my categories and pages:

    public class HomeViewModels 
        {
            [Key]
            public int HomeViewKey { get; set; } //This is a MUST! 
            public IEnumerable<Category> ViewCategories { get; set; }
            public IEnumerable<Page> ViewPages { get; set; }
    
            public void CreateHomeViewModel(IEnumerable<Category> categories,
                                            IEnumerable<Page> pages)
            {
                this.ViewCategories = categories;
                this.ViewPages = pages;
            }        
        }
    

    Then edited my controller to populate the viewmodel:

    public ActionResult Index()
            {
                HomeViewModels homePages = new HomeViewModels();
                homePages.CreateHomeViewModel(db.Categories.ToList(),
                                              db.Pages.ToList());
                return View(homePages);
            }
    

    and finally creating the ul-li lists with the following:

    @{var hvCategories = Model.ViewCategories;}
    @foreach (var categories in hvCategories)
        {
            <li>@Html.ActionLink(categories.Name, "Index", "Home")
                <ul>
                    @{var hvPages = Model.ViewPages
                                    .Where(p => p.CategoryID == categories.CategoryID);}
                    @foreach (var pages in hvPages)
                    {
                        <li>@Html.ActionLink(pages.Name, "Index", "Home")</li>
                    }
                </ul>
        </li>
    

    I hope this helps anyone who plans to build a nested list using a parent-child model. This took me two days to figure out. Cheers!

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

Sidebar

Related Questions

I have the following: public Class Vacancy{ public int VacancyID {get;set;} public List<Application> Applications
I have the following types and classes: namespace MVC.Models public class Page { public
I have the following Models : public class EvaluationCriteriaItem { public int ID {
I have the following model: public class Tag { public int Id { get;
I have the following Model: public class DeliveryTracking { public string TrackingRef { get;
I have a custom class: public class Person { public String Name { get;
I have the following Models and ViewModels (edited for brevity): public class Advert {
I have the following model: public class Contact { public Contact() { Name =
So I have the following model: public class Person { public String FirstName {
Say you have the following object: public class Address { public String Line1 {

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.