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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:06:14+00:00 2026-05-28T14:06:14+00:00

I have a partial view that loops through its Model (a list of things)

  • 0

I have a partial view that loops through its Model (a list of things) to show the thing.Name and three integer values that are counts of related entities.

First of all, I tried putting: (pseudo-razor)

foreach(thing in Model){
    @thing.Name : 
        @thing.related.entities.where(condition1).Count()
        @thing.related.entities.where(condition2).Count()
        @thing.related.entities.where(condition3).Count()
}

But its really slow… so I created a function in the ThingRepository that does same queries faster, something like this (pseudo-code)

function GetCountofRelatedEntities(relatedID,condition){
    return db.entities.where(relatedID==relatedID && condition).count()
}

and its much faster, so I want to call it. I think I should call it from the controller, but then I need a ViewModel to keep a (thing, int, int, int) collection as the model, or I can use the ViewBag extremely to pass the results to the view, but, and here is the question:
Why not simply use the repository from the view? whats wrong with this code in the view? (pseudo-razor)

@repo=new ThingRepository()
foreach(thing in Model){
    @thing.Name : 
        @repo.GetCountofRelatedEntities(thing.relatedID,condition1)
        @repo.GetCountofRelatedEntities(thing.relatedID,condition1)
        @repo.GetCountofRelatedEntities(thing.relatedID,condition1)
}

Could you tell me why I shouldn’t instantiate the repository inside a View? Or I can do it?

  • 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-28T14:06:15+00:00Added an answer on May 28, 2026 at 2:06 pm

    One purpose of the MVC pattern is to provide a structure that fits a wide range of common programming situations. To simplify:

    • Model: Describes the shape of your application, i.e. the parts of your software specific to your business.
    • View: Display the data to the user and transmit user events to the server.
    • Controller: Acts as a middleman between the view and the model.

    What you’re proposing “works,” in the sense that it gets the data on the page where you want it. In the short term, it appears to be saving you time and effort, as you don’t have to bother with controllers, viewbags, etc.

    However, you are breaking the MVC structure in a way that you will probably regret later on. For example, say in a few weeks your boss comes to you and says, “Hey, you know that page you added to display that list of entities? We need to do some filtering and sorting on it. And I need it yesterday.”

    Now you’re faced with a choice: Do I add this filtering logic to my view page and meet the deadline, or do I take the time to move the data access code to a controller and rework my view, at the risk of missing the deadline and breaking what’s already working?

    You’ll probably take the easy way out and add the logic to the view, and now you’ve got a growing mess on your hands. We’ve been down this road with VB6 and Webforms apps with 6,000-line codebehind files. Trust me–you don’t want to go there.

    Another problem is that the MVC structure is well understood by the programming community. If someone else comes along and tries to work on your code, you’re making it harder for them by deviating from the conventional approach.

    The MVC structure is time tested and sound. Until you fully understand its purpose and the benefits it provides, try to follow it closely. It’s not a good idea to break the rules until you have a firm grasp on them.

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

Sidebar

Related Questions

if i have created a view model and have a partial form that is
I have partial view that displays model-specific flash messages. The partial looks like: app/views/mymodel/_flashpartial.erb
I have a partial view that I want to basically take care of itself
I have a partial view that is shared between two controllers and I'm trying
I have a partial view (UserControl) that implements a simple pager in my Asp.Net
I have an ASP.NET MVC Partial View that contains a Html.TextBox that is configured
I'm using MVC2 with VS2010 I have a view that has two partial views
in a partial view I have the following: <%Html.RenderAction(MVC.User.GetComments(Model.UserGroupName)); %> can I render a
Scenario: I have a partial view that is used in several places across my
I have a strongly typed partial view CheckBox.ascx that renders a checkbox. This is

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.