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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:12:02+00:00 2026-05-23T07:12:02+00:00

I have a MVC application which has a Controller that has a recursive method

  • 0

I have a MVC application which has a Controller that has a recursive method which returns an IEnumerable

public static IEnumerable<Category> GetSubCategoriesFor(int catId)
    {
        var subs = _db.Category.Where(c => c.parrent_id == catId);

        foreach (var sub in subs)
        {
            yield return sub;

            // Recursive call
            foreach (var subsub in GetSubCategoriesFor(sub.category_id))
            {
                yield return subsub;
            }
        }

The point is i need my view to show all Categories, subcategories and Questions in subcategories (It’s a questionnarie)

So my question is, how can i call this method from my View??

I have seen some examples where they use Html.Action but i cannot iterate over a string or void

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-05-23T07:12:03+00:00Added an answer on May 23, 2026 at 7:12 am

    This GetSubCategoriesFor method seems to be flattening your hierarchical structure by returning a list which is mixing categories, subcategories, …

    Personally I would use display templates. For example if I have the following view model:

    public class CategoryViewModel
    {
        public int Id { get; set; }
        public IEnumerable<string> Questions { get; set; }
        public IEnumerable<CategoryViewModel> Subcategories { get; set; }
    }
    

    I would populate it in the controller. In my example I have hardcoded the values for demonstration purposes but in your real example those values would obviously come from a database or something and they will be retrieved through a repository:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new[]
            {
                new CategoryViewModel
                {
                    Id = 1,
                    Questions = new[] { "q1.1", "q1.2" },
                    Subcategories = new[]
                    {
                        new CategoryViewModel
                        {
                            Id = 2,
                            Questions = new[] { "q2.1" }
                        },
                        new CategoryViewModel
                        {
                            Id = 3,
                            Questions = new[] { "q2.2", "q2.3" }
                        },
                    }
                },
                new CategoryViewModel
                {
                    Id = 4,
                    Questions = new[] { "q1.3", "q1.4" },
                    Subcategories = new[]
                    {
                        new CategoryViewModel
                        {
                            Id = 5,
                            Questions = new[] { "q2.4" }
                        },
                    }
                },
            };
            return View(model);
        }
    }
    

    and then my ~/Views/Home/Index.aspx view will look like this:

    <%@ Page 
        Language="C#" 
        MasterPageFile="~/Views/Shared/Site.Master"
        Inherits="System.Web.Mvc.ViewPage<IEnumerable<AppName.Models.CategoryViewModel>>" 
    %>
    <ul>
        <%= Html.DisplayForModel() %>
    </ul>
    

    and then I would define a display template for a category (~/Views/Home/DisplayTemplates/CategoryViewModel.ascx):

    <%@ Control 
        Language="C#" 
        Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.CanalViewModel>" 
    %>
    <li>
        <div>
            <h3><%= Html.DisplayFor(x => x.Id) %></h3>
            <%= Html.DisplayFor(x => x.Questions) %>
        </div>
        <ul>
            <%= Html.DisplayFor(x => x.Subcategories) %>
        </ul>
    </li>
    

    Now it is the ASP.NET MVC templated helpers that will take care of looping through the tree structure of categories and show the contents on the view. You could extend this further by defining a complex QuestionViewModel instead of the string I’ve used and by defining a display template for this question ~/Views/Home/DisplayTemplates/QuestionViewModel.ascx it will be rendered for each element of the Questions property of a category.

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

Sidebar

Related Questions

in my MVC application I have a controller (ProjectController) which has an action (create).
I have an application, built using MVC, that produces a view which delivers summary
I have an mvc application that has been coded to use Windows authentication and
I have an MVC application, which has two areas - Administrator, and User. What
I have a CodeIgniter MVC application. I have a model called city that has
I have an .Net MVC application which runs fine if I use the build
I have an ASP.NET MVC-application which I want deployable on both IIS6 and IIS7
I have an MVC application that among other things contains a small Silverlight menu
I have an asp.net mvc application with a route similar to: routes.MapRoute(Blog, {controller}/{action}/{year}/{month}/{day}/{friendlyName}, new
I have an ASP.NET MVC (using the release candidate) application that works with a

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.