0

Have one Doubt In MVC Architecture we can able to pass data from Controller to Directly view without creating strongly typed view, than why there is need to use Models ..?

Is it decreases the Performance of the application if we are saving data in ViewData[""] and use it in Views to display Information . . ?

tereško
  • 58,060
  • 25
  • 98
  • 150
dLcreations
  • 357
  • 2
  • 14
  • Well for one, don't you think it is nicer to work with actual objects than a dictionary? – jflood.net Jun 07 '12 at 07:07
  • Yes Thats the thing also – dLcreations Jun 07 '12 at 12:58
  • In a correctly implemented MVC, the *Model Layer* would observe *View* , and react on changes. While in Model2 MVC (aka Web MVC), the *View* would be requesting data from *Model layer*. So .. basically you are solving the wrong dilemma. Also .. i really hope that you are not one of the drones who thin that "MVC" is a synonym for "ASP.NET MVC 3" .. since there was no language mentioned. – tereško Jun 12 '12 at 01:17
  • @dLcreations Just to add, with Models you have intellisense support, and with ViewData you have to guess how you named your properties.The only thing I ever use ViewBag is to pass Title and Meta data to layout from view. But then there is some logic that you can do in Model that you would otherwise have to stuff in your controller. – formatc Jun 16 '12 at 14:42

1 Answers1

2

Storing data in a collection (ViewData) technically will be ever so slightly slower than passing in a strongly typed model (concrete class), but the difference is extremely small. There is also a tiny increase to the memory footprint (because you need memory for the collection and for the thing(s) in the collection) but again, that should be inconsequential.

Strongly typed models provide a clear contract between the controller and the view. I prefer them in all cases personally.

There can be simple views where the developer feels it unnecessary to create a strong type to represent the controller/view contract. For that need, ViewData exists.

I imagine that ViewData is also used by some to pass extra data not originally envisioned in the strongly typed model. I would encourage refactoring the strong type in such cases rather than passing extra data in ViewData.

There may be "legitimate" uses of ViewData that I'm missing, but I have not come across any thus far.

Eric J.
  • 147,927
  • 63
  • 340
  • 553