I'm working in ASP.NET MVC, but this question is pretty much applicable to any MVC framework (and maybe even others).

In a typical MVC application a request arrives at the controller, which then prepares a model object and then calls the view, passing the model object as a parameter. The view then uses the data in the model to generate a response to the original request. Any data in thew model object can be freely used by the view, but I'm wondering - is it OK for a view to request data from elsewhere, like a direct call to a service or something?

At the first moment the answer seems to be a clear "no, why would you need that? Just add everything the view needs to the model object". But I did arrive at this question while working on a practical project, so here's my situation that causes me doubt:

The view needs a lot of data to be able to display itself. Most notably, there is the data of the actual form fields, and also data for all the dropdowns that are on the form (lists of potential values for different fields).

On the other hand, in my project the views are themeable. That is, different customers (our customers, who have bought our website and use it in their business) will have the views customized to suit their needs. Different customers need different fields displayed, and sometimes the data sources for the same dropdown can differ significantly as well.

One possible way would be to query all possible data on every request and add it all to the model object. But there will be a significant performance penalty for this. Better if only the necessary data was queried, however the controller doesn't know what the view will need.

So I see 2 options:

  1. Allow the view to call external functions that return the value lists for the dropdowns;
  2. In the model object, add delegates (or actually add methods to the model object) which will perform the query when the view asks it to.

Neither approach seems very elegant to me. Are there any other alternatives, or if not, which one is better and why?

2 Answers 2


Obtain the external data in the controller method, and pass the data to the view there.

If you need more finesse, write a repository, service layer or data context that contains the needed data retrieval methods.

Any customization or view switching can also take place in the controller, using helper objects or methods as needed.

The view should only be as smart as necessary to display the data. If the view needs to obtain data in real-time, it can do that by calling a controller method via AJAX.

  • No, that's not the point. The controller doesn't know all the data the view will need. At best it knows that "some of these 10 datasets will be needed", but it doesn't know which ones (or maybe none). I also don't need real-time data obtaining. AJAX could be a solution, but an even slower one (lots of unnecessary requests from the client).
    – Vilx-
    Mar 25, 2015 at 0:02
  • 2
    So why don't you simply provide the controller with the necessary resources so that it does know what it needs? The controller is the place to do this, not the view, unless you plan on building some sort of self-maintaining plugins. Mar 25, 2015 at 0:22
  • I don't want another controller action because I don't want to duplicate a lot of code. This is the case of "95% the same, 5% different". OK, more specifically: the product is an e-commerce website (Internet shop) that integrates with our flagship ERP application. It is licensed by several of our clients who each want some minor modifications to it. At the very least they each want a different look-and-feel, since it's a public website that they will show their clients. However there are also minor functional changes.
    – Vilx-
    Mar 25, 2015 at 8:15
  • To accommodate this, I've added a theming system - each client has their own "theme", which consists of the views and css/images/js. The controllers however stay the same for all clients. The functional changes are (luckily) small so far. For example, one of our clients is a B2B company and it doesn't allow self-pickup. They only deliver directly to their customers. So they want the purchase process to include a dropdown where their customer can select one of their office addresses.
    – Vilx-
    Mar 25, 2015 at 8:19
  • On the other hand, another one of our clients is a B2C company which will have a full-public Internet shop where anyone can come in and purchase stuff. However they don't do deliveries at all. All of their customers will have to drive to one of their warehouses to pick up their merchandise. So they need the dropdown to contain the addresses of their warehouses. Same dropdown, same target field in the DB, different dataset for the dropdown choices.
    – Vilx-
    Mar 25, 2015 at 8:22

OK, I guess I figured out the answer to my own question. If I want to go down this road, I need to provide meta-information together with the selected theme. The controllers can then look at this meta-information to adjust their own behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.