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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:53:51+00:00 2026-05-26T01:53:51+00:00

I have a list of categories and sub categories which is passing from controller

  • 0

I have a list of categories and sub categories which is passing from controller to the view. Now, I want them to be represented in the HTML like following. But, I dont know how can i achieve this by using foreach or table or whatever.

enter image description here

EDIT : Code

public ActionResult Electronics()
    {
        var topCategories = pe.Categories.Where(category => category.ParentCategory.CategoryName == "Electronics").ToList();
        //var catsAndSubs = pe.Categories.Include("ParentCategory").Where(c => c.ParentCategory.CategoryName == "Electronics");
        return View(topCategories);
    }

With this view code, I am just able to pull a vertical list.

@foreach (var cats in Model)
{
<li>@cats.CategoryName</li>
foreach (var subcats in cats.SubCategories)
{
<li>@subcats.CategoryName</li>
}
}

  • 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-26T01:53:52+00:00Added an answer on May 26, 2026 at 1:53 am

    When designing HTML mark-up it is very important to consider semantics. What meaning are you trying to convey? That doesn’t look like tabular data to me so please don’t put it in tables 😛

    Based on your wireframe above, the way I would probably structure this is like this:

    <h1>Category Directory</h1>
    
    <h2><a href="/projectors">Multimedia Projectors</a></h2>
    
    <h2><a href="/audio">Home Audio</a></h2>
    <p>
        <a href="/audio/amps">Amplifiers</a>, <a href="/audio/speakers">Speakers</a>
    </p>
    

    Adjust the hX tags to reflect their position within the document’s hierachy. Remember to only ever have ONE h1 per page (or per <acticle>, or <section> if using HTML5).

    If instead you wind up turning this into something like a Superfish menu then this is the markup that you would use:

    <nav id="category_menu">
        <ul>
            <li>
                <a href="/projectors">Multimedia Projectors</a>
            </li>
            <li>
                <a href="/audio">Home Audio</a>
                <ul>
                    <li>
                        <a href="/audio/amps">Amplifiers</a>
                    </li>
                    <li>
                        <a href="/audio/speakers">Speakers</a>
                    </li>
                </ul>
            </li>
        </ul>
    </nav>
    

    Edit
    Your model is not suitable for creating your desired view, the relationship is bottom-up, but to conveniently construct the view you will want the relationships defined top-down. You need to start by converting the data model into a view model, such as:

    class CategoryViewModel
    {
        string CategoryName { get;set; }
        IList<CategoryModel> SubCategories { get;set; }
    }
    

    and to make this:

    IList<CategoryViewModel> Map(IList<CategoryDataModel> dataModel)
    {
       var model = new List<CategoryViewModel>();
    
       //Select the categories with no parent (these are the root categories)
       var rootDataCategories = dataModel.Where(x => x.ParentCategory == null);
    
       foreach(var dataCat in rootDataCategories )
       {
           //Select the sub-categories for this root category
           var children = dataModel
               .Where(x => x.ParentCategory != null && x.ParentCategory.Name = cat.Name)
               .Select(y => new CategoryViewModel() { CategoryName = y.CategoryName })
               .ToList();
    
           var viewCat = new CategoryViewModel()
           {
               CategoryName = dataCat.CategoryName,
               SubCategories = children
           };
    
           model.Add(viewCat);
       }
    
       return model;
    }
    

    Then your view:

    <h1>Category Directory</h1>
    
    @foreach(var category in Model)
    {
        @Html.Partial("Category", category)
    }
    

    Category partial:

    <h2>@Html.ActionLink(Model.CategoryName, "Detail", new { Model.CategoryName })</h2>
    
    @if(Model.SubCategories.Count> 0)
    {
    <p>
        @for (var i = 0; i < Model.SubCategories.Count; i++)
        {
            var subCat = Model.SubCategories[i];
            @Html.ActionLink(subCat.CategoryName, "Detail", new { subCat.CategoryName })
    
            @if(i < Model.SubCategories.Count - 1)
            {
                <text>,</text>
            }
        }
    </p>
    }
    

    Note that my current solution only supports 2 levels of categories (as per your wireframe). It could however be easily extended to be recursive.

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

Sidebar

Related Questions

I have list of categories , some have sub-category and some have sub-sub-category which
I have a list of categories and want to display them if they are
I have created the function below which is intended to list the sub categories
i have categories list like Designtype Multi Sub cat multi 1 sub cat multi
I have a list of categories that I publish to a UI from database.
I have List I want to sort Desc by Priority, which is int and
i have list of rows that user select and i want to delete them,
I have a list of categories ordered alphabetically but I want to be able
I have a list of categories, each of which has a metacategory. I then
I have a list of categories on my webpage. They are stored on mysql

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.