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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:15:04+00:00 2026-05-28T03:15:04+00:00

Context : Since we are developing in C# MVC3, we wanted to have some

  • 0

Context :
Since we are developing in C# MVC3, we wanted to have some classes designed to handle the tables on a web page. (Pagination / search / etc…).

So we finally found that it could be the best to have the following classes :

The table object that will hold all other object and knows the current page / current search etc… (misc informations)

public class Table<T> where T : IPrivateObject
{
    ...

    public ICollection<Column<T>> Columns { get; set; }
    public ICollection<Row<T>> Rows { get; set; }
    public ICollection<RowMenu<T>> Menus { get; set; }

    public ICollection<T> Items { get; set; }

    public Table(
        ICollection<T> inputItems,
        ICollection<Column<T>> columns, 
        ICollection<RowMenuItem<T>> rowMenuItems,
        ...)
        {
            ...
            this.Columns = columns;
        }

The column object that knows which property should be displayed and and a header value

public class Column<T> where T : IPrivateObject
{
    public string Value { get; set; }
    public Expression<Func<T, object>> Property { get; set; }

    public Column(Expression<Func<T, object>> property, string value)
    {
        this.Property = property;
        this.Value = value;
    }
}

The other classes are not really interesting so i won’t post them here.

In the controller, we use these classes like that :

public ActionResult Index(string search = null, string sort = null, int order = 1, int take = 10, int page = 1)
    {
        ICollection<Person> people = prismaManager.PersonManager.Search(search);
        ICollection<Column<Person>> columns= new List<Column<Person>>();
        columns.Add(new Column<Person>(Person => Person, "Person"));
        columns.Add(new Column<Person>(Person => Person.LastMembershipApproval, "Last Membership approval"));
        Table<Person> table = people.ToTable(columns);
    }

We are now writing a helper that will display the table correctly.
It works well for the header but we face a problem with the Expressions when we want to use the @Html.DisplayFor() helper.

This is what we currently have for the content :

private static string TableRows<T>(HtmlHelper<Table<T>> helper, Table<T> table) where T : IPrivateObject
    {
        StringBuilder sb = new StringBuilder();
        foreach (var item in table.Items)
        {
            sb.AppendLine("<tr>");
            foreach (var column in table.Columns)
            {
                sb.AppendLine("<td>");
                sb.AppendLine(helper.DisplayFor(obj => ??? ).ToString()); // How should I use the Expression that is stored in the column but for the current element ?
                sb.AppendLine("</td>");
            }
            sb.AppendLine("</tr>");
        }
        return sb.ToString();
    }

For this to work, we should set the value of the “Person” parameter from the Expression stored in the column to the current item.

new Column<Person>(Person => Person, "Person"));

How are we supposed to do that ?
Should we (if it is possible) modify the expression to set the value ?
Should we recreate a new Expression using the old one as a basic expression ?

I’ve been searching for 3 days now and I can’t find any answers.

Thanks for your help.

UPDATE :

The problem is (as @Groo & @Darin Dimitrov said) that the Helper is of type HtmlHelper> and not HtmlHelper.
Any idea how I could get an HtmlHelper from a HtmlHelper> ?

UPDATE :

Person class is as following :

public class Person : IPrivateObject
{
    public int Id { get; set; }
    public int? AddrId { get; set; }

    [DisplayName("First Name")]
    [StringLength(100)]
    [Required]
    public string FirstName { get; set; }

    [DisplayName("Last Name")]
    [StringLength(100)]
    [Required]
    public string LastName { get; set; }

    [DisplayName("Initials")]
    [StringLength(6)]
    public string Initials { get; set; }

    [DisplayName("Last membership approval")]
    public Nullable<DateTime> LastMembershipApproval { get; set; }

    [DisplayName("Full name")]
    public string FullName
    {
        get
        {
            return FirstName + " " + LastName;
        }
    }
    public override string ToString()
    {
        return FullName;
    }
}
  • 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-28T03:15:05+00:00Added an answer on May 28, 2026 at 3:15 am

    Here’s how you could proceed. Start by writing a custom view data container implementation which could be as simple as:

    public class ViewDataContainer : IViewDataContainer
    {
        public ViewDataContainer(ViewDataDictionary viewData)
        {
            ViewData = viewData;
        }
    
        public ViewDataDictionary ViewData { get; set; }
    }
    

    and then just instantiate a HtmlHelper<T> which is what you need:

    private static string TableRows<T>(HtmlHelper<Table<T>> helper, Table<T> table) where T : IPrivateObject
    {
        var sb = new StringBuilder();
        sb.AppendLine("<table>");
        foreach (var item in table.Items)
        {
            sb.AppendLine("<tr>");
            foreach (var column in table.Columns)
            {
                var viewData = new ViewDataDictionary<T>(item);
                var viewContext = new ViewContext(
                    helper.ViewContext.Controller.ControllerContext,
                    helper.ViewContext.View,
                    new ViewDataDictionary<T>(item),
                    helper.ViewContext.Controller.TempData,
                    helper.ViewContext.Writer
                );
                var viewDataContainer = new ViewDataContainer(viewData);
                var itemHelper = new HtmlHelper<T>(viewContext, viewDataContainer);
    
                sb.AppendLine("<td>");
                sb.AppendLine(itemHelper.DisplayFor(column.Property));
                sb.AppendLine("</td>");
            }
            sb.AppendLine("</tr>");
        }
        sb.AppendLine("</table>");
        return sb.ToString();    
    }
    

    UPDATE:

    The previous example doesn’t handle value types because the expression in the column is of type Expression<Func<T, object>> and when you are pointing to a value type property the value will be boxed and ASP.NET MVC doesn’t allow such expressions to be used with the template helpers. To remedy this problem one possibility is to test whether the value was boxed and extract the actual type:

    sb.AppendLine("<td>");
    var unary = column.Property.Body as UnaryExpression;
    if (unary != null && unary.NodeType == ExpressionType.Convert)
    {
        var lambda = Expression.Lambda(unary.Operand, column.Property.Parameters[0]);
        sb.AppendLine(itemHelper.Display(ExpressionHelper.GetExpressionText(lambda)).ToHtmlString());
    }
    else
    {
        sb.AppendLine(itemHelper.DisplayFor(column.Property).ToHtmlString());
    }
    sb.AppendLine("</td>");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe are there some settings to disable this in extension context. Since I'm developing
I'm developing a web game in pure Python, and want some simple scripting available
I have a web app using master page and content pages (see the attached
I have three tables: Context, Component and ComponentContext. The ComponentContext table links the Component
Context: I have a WPF App that uses certain unmanaged DLLs in the D:\WordAutomation\MyApp_Source\Executables\MyApp
Context: I need to develop a monitoring server that monitors some of our applications
I am developing a small, internal-use only web application. Given its simple nature and
I don't understand why the ABI is important context of developing user-space applications. Is
I am developing an app for android mobiles that communicates with a json/rest web
I'm developing an intranet where I store data using the System.Web.Caching.Cache implementation of caching.

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.