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?

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.

7

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật