tags:

views:

137

answers:

2

Hi: Am trying to understand the ASP.NET MVC ViewData with respect to its size. Since this object is passed between Controller to View, how big this could be? Say for example, if DataTable is passed from Model, and Controller is going to pass it to View. Is there any best practices OR any one had any bad experience to share here?

Thanks in advance.

A: 

All the data you are talking about is server side. It is not like asp.net viewata; it does not get passed in the request. Generally speaking, the size of the data elements passed from controller to view is not an issue.

Tom Cabanski
My concern is about object creation and garbage collection.
+5  A: 

It is good practice to avoid using ViewData at all, because the form that it used: magic strings with need to unboxing them, and to check for null value before unboxing types that aren't nullable's.

Using ViewModel as a place to put all what the view is using is a good practice.

Mendy
+1 Using a composite view model or FormViewModel is even better as you can add to it with minimal code changes.
griegs
Just one caveat; the null check is going to be there no matter what. Using strongly typed views does not get rid of that scenario.
jfar
@jfar: no for non-nulllable types like int, System.DateTime etc... and non for an unboxing string. (also you can .ToString()).
Mendy
One appropriate use for the ViewData collection is shared data for the site template: the menu, username, etc.
Ryan
@Ryan for this case I'll go with putting one typed ShardModel class in the ViewData. (another solution will be creating a base class to a bunch of related views).
Mendy
@Mendy Yep that is what I meant. ViewData["menu"] is of type MenuViewModel. I'm not really a fan of inheritance in my view models, but it would work also.
Ryan