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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:37:31+00:00 2026-06-09T09:37:31+00:00

I am new to MVC3 and C#. Here is my current model @model Reports.ViewModels.ContentViewModel

  • 0

I am new to MVC3 and C#.

Here is my current model

@model Reports.ViewModels.ContentViewModel
<div>Total Hours: </div>

foreach (var user in Model.Users.ToList()) {
   @Html.Partial("_DirectReports", user)
}

This is working great, I then have HTML in these partial view that renders everything I want using another @model.
The problem is I want to add all the data together.

I want to create a var outside the foreach
The foreach inserts data into the var and return it after the foreach is complete like so

@model Reports.ViewModels.ContentViewModel

@{var Total = 0}
foreach (var user in Model.Users.ToList()) {
   @Html.Partial("_DirectReports", user)
   //@Total is assigned a value in the partial view
}

<div>Total Hours: @Total</div>

If this is possible that would be great thks

  • 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-09T09:37:32+00:00Added an answer on June 9, 2026 at 9:37 am

    You’re going down a slippery slope here by mixing logic and your view together. MVC is designed to reduce the coupling between the different layers (the model, the view and the controller) in order to make the code easier to maintain and easier to test, amongst other things.

    So let’s try to clean that up a bit. Firstly, Total is a piece of data and that means it should be a part of the model:

    public class ContentViewModel 
    {
        public List<User> Users { get; set; }
        public int TotalHours
        {
            get
            {
                return Users.Sum(u => u.Hours); // Assuming User has an Hours property
            }
        }
    }
    

    The controller should then be handling the creation of the model and giving it data. Something like this:

    public ActionResult Index()
    {
        ContentViewModel viewModel = new ContentViewModel();
        viewModel.Users = db.Users.ToList();
    
        return View(viewModel);
    }
    

    Now that you have the data you need, you can cut down on a lot of the logic you’re currently using in the view (such as the use of the foreach and the Total var):

    @model Reports.ViewModels.ContentViewModel
    
    @Html.DisplayFor(m => m.Users)
    
    <div>Total Hours: @Model.TotalHours</div>
    

    Html.DisplayFor and Html.EditorFor use what are called display/editor templates and they will automatically loop over collections for you, rendering a template for each item. In order to take advantage of this for ContentViewModel, you need to create a display template. To do this, you need to create a folder under where the view is, and name it DisplayTemplates. For example, if your view is ~/Views/Home/Index.cshtml, you need to create the folder: ~/Views/Home/DisplayTemplates.

    Right-click that folder to add a new view. In the dialog box that appears, you want to make a strongly-typed view, selecting the type you want to pass to the template (in my example, User is the type and not List<User>), make it a partial view and make sure to give it the same name as the type (again, User). Once you have your template, you can use the HTML helpers just as you would in a normal view. Something like this:

    @model Models.User
    
    @Html.LabelFor(m => m.Name)
    @Html.DisplayFor(m => m.Name)
    

    Now you have things broken up into manageable chunks. If you’d like more information on using display/editor templates, I’d highly suggest reading Brad Wilson’s article series on the subject. Even though it was written for MVC 2, it still applies.

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

Sidebar

Related Questions

Using ASP.Net C# with MVC3 Here is my insert code. var fInfo = new
My new MVC3 web application allows users to store some information about themselves, and
When I create a new MVC3 application, add a model, add a [Required] attribute
I am using MS Visual Studio 2010. When i create a new MVC3 project
New to MVC3 Razor - linq to sql having spent some time trying to
I am new in MVC3. When i create a new default ASP.NET MVC 3
I'm new to mvc3. I created one web project though mvc3 . On Index
I am new in MVC3 i am going to create a MVC3 test project
I am new in MVC3. When i create a strongly typed view it use
I'm new to MVC3 and I can't figure out how to use checkboxes in

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.