1

Problem:

If I have a scenario where I don't want to change my model properties and need to get one more attribute from some table, is it fine if I write a query inside a view and show the result?

If the answer is no, I would like to know why it's not considered a good way.

1
  • 2
    Why not query in the controller and put the data in ViewBag, or ViewData. Because regarding MVC principle you must separate concern of the View, Model and the Controller Commented Apr 15, 2014 at 9:52

5 Answers 5

5

In my opinion you should never do that. Just because you loose the entire paradigm of the tool you are using.

In a few years, this is confusing to you, or someone else maintaining the code. If you don't want to change it all, use dynamic properties, ViewBag or other options to keep your flexibility, test-ability, etc.

Sign up to request clarification or add additional context in comments.

Comments

3

Yes.

View is not a mirror of your data structure. Actually, often it's an aggregation and/or manipulation for user-friendly presentation of the data that stands behind.

6 Comments

But shouldn't all the data stand behind then ? Meaning that if you change your datamodel, you should adapt your model accordingly ? Innocent question, still discovering MVC myself... but putting database queries in a view seems in contradiction to the MVC principles... no ?
You change Model to meet data changes, but you may not change a View. And vice versa. I can change View of the data, for newer UI, new control introduced, new set of screens has to be supported...etc, but the data remains the same.
but there is some scenario where i want to check some condition based on db result that this particular element to be rendered or not, then what should be done?
@EhsanSajjad: there are no rigid rules on this. you can do it in view, or in controller. it depends on who is processing the data in your architecture.
@Bartdude: again, it depends. If he has a constrain of not changing a model to meet new requirements, for some reason. View may become a suitable layer, were you if/else data and send to client html based on some condition
|
3

In my opinion, you should never do queries in your Views. It's called MVC and the proper way of using it is to separate concepts.

You should have a Model, a Controller, a View and a Repository (as the simpliest approach). As the name suggests, View must be used only for displaying processed data.

More information about MVC best practices is here.

I remember when I started working with MVC. I didn't know about this and my teamleader told me it's never a good choice to do this.

Conclusion:

It's just a matter of design and perspective. If you're doing a project for yourself or for the university, organize your code as you wish. If you're working in a professional environment you should respect some working principles

Another important factor to think of, is this: if another person must refactor or modify your code he must know where to find methods, where to find models and so on.

2 Comments

Thank you. I've updated the answer ( fixed some grammar problems :) )
i accepted @Tigran answer as he answered good in a single sentence. but yoyrs was also good one thats why i voted up..:)
2

First of all, the reason why it's called MVC is because of the separation of concerns. View's are only concerned of the User-Interface. Queries are done on the controller. Another is it would be too confusing if you would place everything in a single page which already defeats the purpose of having MVC.

Comments

1

Though there are cases when querying the database from a View may seem easier and appropriate, you should be ready for the consequences of breaking separation of concerns (SoC). When you spread your DAL across many layers it's going to be hard to re-factor the database in the future. Another question is security - if you decide to implement database security or controller-based security later, your view-based queries will be a security point.

However, if the project is small, I would trade SoC for performance.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.