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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:32:49+00:00 2026-06-11T14:32:49+00:00

Is there in Razor a way to print some HTML on the page while

  • 0

Is there in Razor a way to print some HTML on the page
while the value later known in the view?

For example: I would like to print the sum of an expensive calculation, and that sum should be before the items in the HTML. But with the example below, it will always print 0.
I would like to calculate the sum only once.

I would like to solve this in the view, not in a csharp helper or on the client side (css, javascript etc)

@{
  var sum = 0;
}

<table>
@* I would like to print here the sum which is known after the foreach loop*@
 <tr><td>total: @sum</td></tr>

    @foreach (var item in items)
    {
      <tr>
        @{ 
          var x= item.expensiveCalculation(); 
          sum+= x;
        }
        //print item with x
        <td>@x</td>

      </tr>
    }

</table>

edit: It is very important that the expensiveCalculation() is only calculated once for each item!

  • 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-11T14:32:50+00:00Added an answer on June 11, 2026 at 2:32 pm

    Your model is not adapted to the requirements of the view. Full stop.

    So when your model is not adapted to the requirements of your view you go ahead and define a view model so that all the expensive operations are done inside the controller.

    So:

    public class ItemViewModel
    {
        public decimal Price { get; set; }
        public string Name { get; set; }
    }
    

    Now your view becomes strongly typed to the view model and there are no longer expensive operations there:

    @model IEnumerable<ItemViewModel>
    <table>
        <tr>
            <td>
                total: @Model.Sum(item => item.Price)
            </td>
      </tr>
      @foreach (var item in Model)
      {
          <tr>
              <td>@item.Name - @item.Price<td>
          </tr>
      }
    </table>
    

    and now inside your controller action prepare this view model and pass it to the view:

    public class SomeAction()
    {
        IEnumerable<Item> items = ... go and fetch your domain models from wherever you are fetching them
        // now let's build the view model
        var model = new MyViewModel
        {
            Items = items.Select(x => new ItemViewModel
            {
                Name = x.Name,
                Price = x.expensiveCalculation()
            })
        };
    
        // and we are obviously passing the view model to the view, not the domain model
        return View(model);
    }
    

    As you can see we are doing the expensive operations for each element inside the controller in order to bind it to the corresponding property of the view model (Price) but we are no longer paying this expensive operations price inside the view as we are simply summing over the view model pre-calculated properties.

    And next time when you encounter a problem in ASP.NET MVC don’t forget that view models are the solution to your problem.

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

Sidebar

Related Questions

is there some way to share a partial razor view between areas? For example
Is there way to set razor @HTML.Hidden control value within a JavaScript code block?
Is there a way to create functions like these in the Razor view engine?
I would like to have my Razor View return a string of the html
Is there any way to refresh some part of page (e.g div/span) on selection
Writing @Url.Content(~/Something/Something.html) in razor renders /AppFolder/Something/Something.html Is there a way to render the full
Is there a way to execute a razor view dynamically? If I have the
Is there a server-side way to cache at page level in Razor c# in
This is my cshtml file in WebMatrix (Razor syntax). Is there some way to
Using the new ASP.NET MVC 3.0 Razor View Engine, is there any way to

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.