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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:07:51+00:00 2026-05-16T23:07:51+00:00

I have a feeling I’m doing this horribly, horribly wrong. Nested for loops? What

  • 0

I have a feeling I’m doing this horribly, horribly wrong. Nested for loops? What is the best practice method of listing subcategories? I have a feeling it involves preparing the list in my controller action and sending it to the client via some actionresult, but I don’t know where to start? Anybody able to point me in the right direction? Here’s my hacky code:

 <h2>Categories</h2>
    <a href="javascript:;" onclick="newCategory()">Create New Category</a>
<br />
    <ul class="parent">
        <%foreach (var category in Model.Categories){%>
            <%-- List all of the top-level parent categories --%>
            <%if (category.IsParent && category.ParentId == 0)%>
            <li>
                <span class="buttons"><a href="javascript:;" onclick="editCategory(<%:category.CategoryId%>)" class="edit"></a> <a href="javascript:;" onclick="deleteCategory(<%:category.CategoryId%>)" class="delete"></a></span>
                <span class="categoryName"><%:category.CategoryName%></span>
                <span class="positionButtons"><%:Html.ActionLink(" ", "MoveCategoryUp", new {id = category.CategoryId},
                                                new {Class = "moveUp"})%><%:Html.ActionLink(" ", "MoveCategoryDown", new {id = category.CategoryId},
                                                new {Class = "moveDown"})%></span>
                <%-- List all of the subs for each parent --%>

                    <ul>
<%-- Level 1 --%>       <%foreach (var sub1 in Model.Categories){%>
                            <%if (sub1.ParentId == category.CategoryId){%>
                                <li>
                                    <span class="buttons"><a href="javascript:;" onclick="editCategory(<%:sub1.CategoryId%>)" class="edit"></a> <a href="javascript:;" onclick="deleteCategory(<%:sub1.CategoryId%>)" class="delete"></a></span>
                                    <span class="categoryName"><%:category.CategoryName%></span>
                                    <span class="positionButtons"><%:Html.ActionLink(" ", "MoveCategoryUp", new {id = sub1.CategoryId},new {Class = "moveUp"})%><%:Html.ActionLink(" ", "MoveCategoryDown", new {id = sub1.CategoryId},new {Class = "moveDown"})%></span>

                                    <%-- List all of the subs for each parent --%>
                                    <%if (sub1.IsParent){%>
                                    <ul>
<%-- Level 2 --%>                       <%foreach (var sub2 in Model.Categories){%>
                                            <%if (sub2.ParentId == sub1.CategoryId){%>
                                                <li>
                                                    <span class="buttons"><a href="javascript:;" onclick="editCategory(<%:sub2.CategoryId%>)" class="edit"></a> <a href="javascript:;" onclick="deleteCategory(<%:sub2.CategoryId%>)" class="delete"></a></span>
                                                    <span class="categoryName"><%:category.CategoryName%></span>
                                                    <span class="positionButtons"><%:Html.ActionLink(" ", "MoveCategoryUp", new {id = sub2.CategoryId},new {Class = "moveUp"})%><%:Html.ActionLink(" ", "MoveCategoryDown", new {id = sub2.CategoryId},new {Class = "moveDown"})%></span>
                                                    <%-- List all of the subs for each parent --%>
                                                    <%if (sub2.IsParent){%>
                                                    <ul>
<%-- Level 3 --%>                                       <%foreach (var sub3 in Model.Categories){%>
                                                            <%if (sub3.ParentId == sub2.CategoryId){%>
                                                                <li>
                                                                    <span class="buttons"><a href="javascript:;" onclick="editCategory(<%:sub3.CategoryId%>)" class="edit"></a> <a href="javascript:;" onclick="deleteCategory(<%:sub3.CategoryId%>)" class="delete"></a></span>
                                                                    <span class="categoryName"><%:category.CategoryName%></span>
                                                                    <span class="positionButtons"><%:Html.ActionLink(" ", "MoveCategoryUp",new {id = sub3.CategoryId},new {Class = "moveUp"})%><%:Html.ActionLink(" ", "MoveCategoryDown",new {id = sub3.CategoryId},new {Class = "moveDown"})%></span>

                                                                     <%-- List all of the subs for each parent --%>
                                                                    <%if (sub3.IsParent){%>
                                                                    <ul>
<%-- Level 4 --%>                                                       <%foreach (var sub4 in Model.Categories){%>
                                                                            <%if (sub4.ParentId == sub3.CategoryId){%>
                                                                                <li>
                                                                                    <span class="buttons"><a href="javascript:;" onclick="editCategory(<%:sub4.CategoryId%>)" class="edit"></a> <a href="javascript:;" onclick="deleteCategory(<%:sub4.CategoryId%>)" class="delete"></a></span>
                                                                                    <span class="categoryName"><%:category.CategoryName%></span>
                                                                                    <span class="positionButtons"><%:Html.ActionLink(" ", "MoveCategoryUp", new {id = sub4.CategoryId}, new {Class = "moveUp"})%><%:Html.ActionLink(" ", "MoveCategoryDown", new {id = sub4.CategoryId}, new {Class = "moveDown"})%></span>

                                                                                    <%-- If more than 4 levels of subcategories are required, put another level here --%>
                                                                                </li>
                                                                            <%}%>
                                                                        <%}%>
                                                                    </ul>
                                                                    <%}%>
                                                                </li>
                                                            <%}%>
                                                        <%}%>
                                                    </ul>
                                                    <%}%>
                                                </li>
                                            <%}%>
                                        <%}%>
                                    </ul>
                                    <%}%>
                                </li>
                            <%}%>
                        <%}%>
                    </ul>
            </li>

        <%}%>

    </ul>

Edit

Unfortunately this code isn’t rendering the results I’m looking for, so I cant really provide much more than this: http://jsfiddle.net/EeaGr/ each list item has buttons for edit/delete and moveup/movedown options for its category. My category has the following properties:

CategoryID : int

Name: string

ParentID : int

IsParent : bool

Position: int

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

    First, I would change the structure of categories, so that each Category has a Subcategories property.

    Then, you should create a User Control that renders one Category and if that category has subcategories, it calls itself recursively:

    CategoryControl.ascx:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Category>" %>
    <%@ Import Namespace="so_subcats.Model" %>
    <li><%= Model.Name %>
        <% if (Model.Subcategories != null) { %>
            <ul>
            <% foreach (Category subcat in Model.Subcategories)
                 Html.RenderPartial("CategoryControl", subcat); %>
            </ul>
        <% } %>
    </li>
    

    Then just create a View that renders this control for each of the top-level categories:

    Categories.aspx:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
      Inherits="System.Web.Mvc.ViewPage<IEnumerable<Category>>" %>
    <%@ Import Namespace="so_subcats.Model" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
        Categories
    </asp:Content>
    
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
        <h2>Categories</h2>
            <ul>
            <% foreach (Category cat in Model)
                 Html.RenderPartial("CategoryControl", cat); %>
            </ul>
    
    </asp:Content>
    

    Of course, if you don’t want to change the structure of your classes, you can use this solution too, you just have to modify it slightly.

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

Sidebar

Related Questions

I have a feeling I'm going about this all wrong. But anyway. I have
I have a feeling I'm doing something wrong here, but I'm not quite sure
I have a feeling this XML is not valid, can someone please explain why?
I have the feeling the answer to this question is no but I figured
I have a feeling that this query is pretty easy to construct, I just
I have a feeling this is a stupid question but I can't find the
I have a feeling I am completely borking this MySQL query but I'll ask
I have feeling, that if one defines a method def test puts 'Hi' end
I have a feeling I'm missing something pretty simple here but, in this one
I have a feeling I may already know the answer to this question, but

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.