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

  • Home
  • SEARCH
  • 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 7084157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:16:00+00:00 2026-05-28T07:16:00+00:00

So I have built a recursive function that generates a collection of Category objects.

  • 0

So I have built a recursive function that generates a collection of Category objects.

[ChildActionOnly]
public ActionResult FindAllCategorias()
{
    var categoriasDb = _categoriaRepository.FindAllCategorias().Where(s => s.CategoriaPadreId == null);
    List<CategoriaModel> model = new List<CategoriaModel>();

    foreach (var categoria in categoriasDb)
    {
            model.Add(new CategoriaModel()
                            {
                                CategoriaId = categoria.CategoriaId,
                                Nombre = categoria.Nombre,
                                Encabezado = categoria.Encabezado
                            });
    }

    foreach (var categoriaModel in model)
    {
        categoriaModel.Subcategorias = FindSubcategoriesForCategory(categoriaModel.CategoriaId);
    }

    return PartialView(model);
}

private List<CategoriaModel> FindSubcategoriesForCategory(int id)
{
    var subcategorias = _categoriaRepository.FindAllCategorias().Where(c => c.CategoriaPadreId == id);

    List<CategoriaModel> subcategoriasModel = new List<CategoriaModel>();

    foreach (var subcategoria in subcategorias)
    {
        subcategoriasModel.Add(new CategoriaModel()
                                    {
                                        CategoriaId = subcategoria.CategoriaId,
                                        Nombre = subcategoria.Nombre,
                                        Encabezado = subcategoria.Encabezado,
                                        Subcategorias = FindSubcategoriesForCategory(subcategoria.CategoriaId)
                                    });
    }

    return subcategoriasModel;
}

Now in my View, how do you suggestion I use recursion to spit out each Category in a template I choose? I’m not sure how to something like this in a View.

  • 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-28T07:16:00+00:00Added an answer on May 28, 2026 at 7:16 am

    You could use a recursive display template:

    @model List<CategoriaModel>
    <ul>
        @Html.DisplayForModel()
    </ul>
    

    and then define a custom display template for a category (~/Views/Shared/DisplayTemplates/CategoriaModel.cshtml):

    @model CategoriaModel
    <li>
        @Html.DisplayFor(x => x.Encabezado) ... and something else about the category  
        <ul>
           @Html.DisplayFor(x => x.Subcategorias)
        </ul>
    </li>
    

    You may also find the following post useful in terms of optimizing your code and data access.

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

Sidebar

Related Questions

I have a strongly recursive function, that creates a (very small) std::multimap locally for
I have built an MSI that I would like to deploy, and update frequently.
I have built an application in C# that I would like to be optimized
I have built a .dll under WinXP that claims it can't find DWMAPI.DLL when
I have built a MS Access 2007 application that can create reports files in
I have built an application that uses SQL Express 2005 and I want to
I have an MVC view with a partial view recursive call that displays hierarchical
Is there a built in function in jQuery that would allow me to get
I have a python program that uses a custom-built DLL. This DLL crashes due
I've written a recursive function, which generates an organizational diagram by looking at each

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.