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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:29:48+00:00 2026-05-23T12:29:48+00:00

UPDATED I am trying to create an HtmlHelper Extension that has the ability to

  • 0

UPDATED

I am trying to create an HtmlHelper Extension that has the ability to take a collection of TModels. Then set an Expression that will grab the declared property foreach item in the collection and print them out.

This code here below does work. However, the one thing I don’t like in the pattern I currently have is the generic types are not inferred. I have to declare the types for my extension method.

I am trying to follow the type of pattern that Telerik used for their Grid, where when you declare a model, the generic types are then inferred.

I would like to do this in my view:

@model List<MyProject.MyModels.UserModel>
@(Html.MyExtensions()
      .PrintUsers(Model)
      .FirstName(m => m.FirstName)
      .LastName(m => m.LastName)
)

With my current pattern I have to do this:

@model List<MyProject.MyModels.UserModel>
@(Html.MyExtensions<List<MyProject.MyModels.UserModel>, MyProject.MyModels.UserModel>()
      .PrintUsers(Model)
      .FirstName(m => m.FirstName)
      .LastName(m => m.LastName)
)

So I need to figure out a better pattern so my types are inferred.
Any ideas?

UserModel looks like:

public class UserModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

So I have an HtmlHelper that looks like this:

public static class UIExtension
{
    public static ComponentFactory<TModel, T> MyExtensions<TModel, T>(this HtmlHelper<TModel> html) 
        where TModel : IEnumerable<T>
        where T : class
    {
        return new ComponentFactory<TModel, T>(html);
    }
}

So one of my components would take a List and then iterate thru printing out each declared property from a Linq Expression to the page.

In my ComponentFactory I have this:

public class ComponentFactory<TModel, T>
    where T : class
{
    private HtmlHelper<TModel> _html;

    public ComponentFactory()
    {
    }

    public ComponentFactory(HtmlHelper<TModel> html)
    {
        this._html = html;
    }

    public ListComponent<T> PrintUsers(IEnumerable<T> model)
    {
        return new ListComponent<T>(model);
    }

    /* Add other ui components to this factory */
}

Then my ListComponent

public class ListComponent<T> : IHtmlString
{
    private Expression<Func<T, string>> _firstname;
    private Expression<Func<T, string>> _lastname;

    private IEnumerable<T> _model;

    public ListComponent(IEnumerable<T> model)
    {
        this._model = model;
    }

    public ListComponent<T> FirstName(Expression<Func<T, string>> property)
    {
        this._firstname = property;
        return this;
    }

    public ListComponent<T> LastName(Expression<Func<T, string>> property)
    {
        this._lastname = property;
        return this;
    }

    public override MvcHtmlString Render()
    {
        TagBuilder container = new TagBuilder("div");

        TagBuilder title = new TagBuilder("div");
        title.InnerHtml = "<b>Names</b>";
        container.InnerHtml = title.ToString(TagRenderMode.Normal);

        TagBuilder ul = new TagBuilder("ul");

        foreach (var item in this._model)
        {
            TagBuilder li = new TagBuilder("li");
            li.SetInnerText(String.Format("{0} {1}", _firstname.Compile()(item), _lastname.Compile()(item)));
            ul.InnerHtml += li.ToString(TagRenderMode.Normal);
        }

        container.InnerHtml += ul.ToString(TagRenderMode.Normal);

        return MvcHtmlString.Create(container.ToString(TagRenderMode.Normal));
    }
}

Thanks for any help and suggestions!

  • 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-23T12:29:49+00:00Added an answer on May 23, 2026 at 12:29 pm

    After taking a look at @Tomas’ answer, it helped me realize what I was missing in the pattern. Although @Tomas’ answer did get me the syntax I wanted in the View where the Model was being inferred correctly, the only draw back was the entire ViewModel was tehn tied to an IEnumerable<T>. Since I would like to bind to a property in my ViewModel that is of IEnumerable I made a couple changes and it worked out.

    The HtmlHelper Extension:

    public static class UIExtension
    {
       public static ComponentFactory MyExtensions(this HtmlHelper html)
       {
           return new ComponentFactory(html);
       }
    
       public static ComponentFactory<TModel> MyExtensions<TModel>(this HtmlHelper<TModel> html) 
          where TModel : class
       {
           return new ComponentFactory<TModel>(html);
       }
    }
    

    The ComponentFactory:

    public class ComponentFactory<TModel>
    {
       private HtmlHelper<TModel> _html;
       public ComponentFactory(HtmlHelper<TModel> html)
       {
          this._html = html;
       }
    
       public ListComponent<TObj> PrintUsers<TObj>(IEnumerable<TObj> model)
       {
          return new ListComponent<TObj>(model);
       }
    }
    

    So the main change is that for the PrintUsers method, I use a different Generic Type to handle that portion. This way my Model can be what ever the developer decides and they simply set the Property to use as the datasource for the ListComponent.

    So now in the View it’s very flexible, I can do something like this…

    @(Html.MyExtensions()
          .PrintUsers(Model.MyList)
          .FirstName(m => m.FirstName)
          .LastName(m => m.LastName)
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a Makefile that will download and process file a file
I'm trying to create a simple scope that sphinx will index (Ruby on Rails).
Trying to create an Oracle trigger that runs after a table is updated in
I'm trying to create a stored proc that returns all records that are updated.
I am trying to create a Trigger, that will update the History Table with
I'm trying to create a page that has some information in databound labels with
I'm trying to create an MVC view that will display some data in a
I'm trying to create a regular expression that matches when the string starts with
I am trying to get all users that are updated maximum 90 seconds ago:
I'm trying to create an ASP.NET/C# page that allows the user to edit 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.