The default mechanism for passing data from an ASP.NET MVC controller to the view is to use the ViewData property of the controller and view which is of type ViewDataDictionary. The ViewDataDictionary is a string indexed collection of type object. My main issue with this mechanism is two fold. I’m not a big fan of string indexed collections and I don’t want to have to cast to get at the type of the class instance in the collection.
The next option is to use the Model property of the ViewDataDictionary. The Model property of ViewData allows a strongly typed way to pass data from the controller to the view. The question becomes what type should be used? One choice is to use a specific type for each controller action and view, but this gets a bit cumbersome. Instead I propose using a collection indexed by type which is assigned to the Model property of ViewData.
I created a project called TTC Tools which houses the ITypeInstanceDictionary, which provides a generic mechanism to pass various types from controller to view in a more strongly typed way than the ViewDataDictionary.
I documented some basic usage scenarios for the type instance dictionary on my wiki. I also created a screen cast to document basic usage in an ASP.NET MVC application.
The basic idea is the dictionary provides the template functions Add<T>, Get<T>, and Contains<T>. The type T is used as the indexer of the dictionary, so instances of various types can be added and retrieved from the dictionary.
A very basic usage scenario from a unit test is illustrated as follows:
The type does not have to be specified for the Add call, as the declared type of the parameter will be used. The Get call must declare the type, as the type T is used for the dictionary index.
The type instance dictionary does allow multiple instances of the same type to be added, but only if some other type of instance identifier is supplied. For example an enumeration can be used to differentiate the types. An example of adding and retrieving multiple instances of the same type using an enumerated type for the instance id follows:
Basic use in an ASP.NET MVC application
For convenience, derive all your controllers from a base controller that provides a property of type ITypeInstanceDictionary. My example uses a property named TypeViewData. The code for the base controller follows:
public class BaseController : Controller
{
ITypeInstanceDictionary _typeViewData = new TypeInstanceDictionary();
public ITypeInstanceDictionary TypeViewData
{
get
{
return _typeViewData;
}
set
{
_typeViewData = value;
}
}
}
Controller actions that return a ViewResult should use the controller View function overload which accepts the model argument. The TypeViewData property should be passed into the view function as follows:
The view should then use the template version of the ViewPage class, with the type set to TTC.Tools.ITypeInstanceDictionary. The template version of the ViewPage class strongly types the Model property of the View. The Model property can then use the Get<T> function to retrieve the data sent from the controller. An example of the view code follows:
You can find the code, which includes a sample web site using the type instance dictionary, at the TTC Tools GitHub page. I’ve also created some basic documentation on my wiki.
If you have any questions please let me know! I’m not sure if others will find this useful, but you never know 