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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:44:17+00:00 2026-05-19T17:44:17+00:00

I have built a little repeater HtmlHelper for asp.net MVC and I would like

  • 0

I have built a little repeater HtmlHelper for asp.net MVC and I would like to be able to recurse through to build an tree style list. So if <T> has any children that are IEnumerable I would like to add them as embedded lists.

Here is what I have so for which creates a flat list…

    public static MvcHtmlString Repeater<T>(this HtmlHelper html, IEnumerable<T> items,
        Func<T, HelperResult> itemTemplate,            
        Func<string, HelperResult> containerTemplate,
        Func<string, HelperResult> emptyTemplate)
    {
        if (items == null)
            return MvcHtmlString.Create(HttpContext.Current.Server.HtmlDecode(emptyTemplate("No Results").ToHtmlString()));

        if (items.Count() == 0)
            return MvcHtmlString.Create(HttpContext.Current.Server.HtmlDecode(emptyTemplate("No Results").ToHtmlString()));

        StringBuilder sb = new StringBuilder();

        foreach (var item in items)
        {
            string content = itemTemplate(item).ToHtmlString();

            //Here I would want to append the children to 'content' using containerTemplate and itemTemplate....

            sb.Append(HttpContext.Current.Server.HtmlDecode(content));
        }

        return MvcHtmlString.Create(HttpContext.Current.Server.HtmlDecode(containerTemplate(sb.ToString()).ToHtmlString()));
    }

Any help would be greatly appreciated.

Cheers in advance.

Gifster

  • 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-19T17:44:18+00:00Added an answer on May 19, 2026 at 5:44 pm

    I would approach this using recursion. I doubt if simply appending the string to content will be enough. You probably need to parse the HTML from the template and insert it into the template in an intelligent fashion depending on whether it’s a list or a table, etc. I’m omitting the implementation of that method since I don’t know how you expect it to be used. I am showing how to use HtmlAgilityPack, though, for the parsing.

    using HtmlAgilityPack;
    
    public static MvcHtmlString Repeater<T>(this HtmlHelper html, IEnumerable<T> items,
        Func<T, HelperResult> itemTemplate,            
        Func<string, HelperResult> containerTemplate,
        Func<string, HelperResult> emptyTemplate)
    {
        if (items == null)
            return MvcHtmlString.Create(HttpContext.Current.Server.HtmlDecode(emptyTemplate("No Results").ToHtmlString()));
    
        if (items.Count() == 0)
            return MvcHtmlString.Create(HttpContext.Current.Server.HtmlDecode(emptyTemplate("No Results").ToHtmlString()));
    
        StringBuilder sb = new StringBuilder();
    
        var enumerableProperties = typeof(T).GetProperties()
                                            .Where( p => p is IEnumerable );
    
        foreach (var item in items)
        {
            var doc = new HtmlDocument();
            doc.LoadHtml( itemTemplate(item).ToHtmlString() );
            var root = doc.DocumentNode.FirstChild;
            var insertNode = FindInsertNode( doc ); // this needs to be written        
            foreach (var property in enumerableProperties)
            {
                 var value = property.GetValue( item, null ) as IEnumerable;
                 var newNode = new HtmlNode
                               {
                                   InnerHtml = html.Repeater( value, itemTemplate, containerTemplate, emptyTemplate )
                               };
                 root.InsertAfter( newNode, insertNode );
                 insertNode = newNode;
            }
            sb.Append(html.InnerText);
        }
    
        return MvcHtmlString.Create(HttpContext.Current.Server.HtmlDecode(containerTemplate(sb.ToString()).ToHtmlString()));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built a little daemon in Java and I would like to run
On an ASP.NET-MVC2 project, I have built some user controls that resides in Views\Shared\EditorTemplates
I have built a little web application with several *.js files. I am using
We have a little in-house LMS with courses built in Flash. Scores are updated
Good day. I have little usage of Flash CS4, however i have to build
I have this nice little MSBuild-based daily build setup that I use on my
I have a site I'm trying to build and I've hit one little snag
In our infrastructure, we have lots of little Java projects built by Maven2. Each
I have built a little Web UI for Pidgin(respectively all libpurple based messengers) together
I have built a variety of little scripts using Ruby's very simple Queue class,

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.