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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:59:05+00:00 2026-06-11T13:59:05+00:00

I have a table built out in a Razor view that goes like so:

  • 0

I have a table built out in a Razor view that goes like so:

@foreach (var item in Model)
{
    <tr>
        <td>@Html.DisplayFor(modelItem => item.column1)</td>
        ...
        ...
    </tr>
}

I want to add a summary of the table at the end, something like:

<tr>
    <td>@Model.Sum(a => a.column1)</td>
    ...
    ...
</tr>

That actually works but it doesn’t use my data annotations since I’m not using DisplayFor(). I tried placing the Model.Sum within a DisplayFor() but the code doesn’t work. Can anyone point me towards a solution?

  • 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-06-11T13:59:07+00:00Added an answer on June 11, 2026 at 1:59 pm

    You could write an HTML helper that examined the property lambda, performed the sum() and utilized your data annotations.

    View Model

    public class FooViewModel
    {
        [Required]
        [Display( Name = "My Display Name" )]
        public int Bar {
            get;
            set;
        }
    
        public string Baz {
            get;
            set;
        }
    }
    

    View

    @model IEnumerable<FooViewModel>
    
    @* Razor engine prefers this syntax? think generic may confuse it *@
    @(Html.SumFor<FooViewModel>( o => o.Bar ))
    

    Helper Extension

    This is far from perfect. An improvement would be to allow any type to be summed without having to provide a different method for every summable type.

    public static IHtmlString SumFor<TEnumerableItem>( this HtmlHelper helper, 
        Expression<Func<TEnumerableItem, int>> expression ) {
    
        // get metadata through slightly convoluted means since the model
        // is actually a collection of the type we want to know about
    
        // lambda examination
        var propertyName = ( (MemberExpression)expression.Body ).Member.Name;
    
        // property metadata retrieval
        var metadata = ModelMetadataProviders.Current
            .GetMetadataForProperty( null, typeof( TEnumerableItem ), propertyName );
    
        // make an assumption about the parent model (would be better to enforce 
        // this with a generic constraint somehow)
        var ienum = (IEnumerable<TEnumerableItem>)helper.ViewData.Model;
    
        // get func from expression
        var f = expression.Compile();
    
        // perform op
        var sum = ienum.Sum( f );
    
        // all model metadata is available here for the property and the parent type
        return MvcHtmlString.Create( 
           string.Format( "<div>Sum of {0} is {1}.</div>", metadata.DisplayName, sum )
        );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a view file that is built like this: <html> ...
When trying to pass a table built with HTML in my servlet like that:
I have an HTML table containing a few rows (this is built dynamically). All
I have a table that looks like this: Col1 Col2 Col3 Col4 a b
I have a table that is dynamically built using DOM. It has 10 cols,
I have an existing table that I'd like to use for a Rails application.
I have an application that is essentially built out of many smaller applications. Each
I have LINQ query that is built up in a piecemeal fashion like so:
I have built a C++ dll that I would like to call from C#
I have built a table which is basically done by HorizontalScrollView inside a ScrollView

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.