tags:

views:

68

answers:

2

is it possible to pass derived types where base types are expected to strongly typed views?

I.e

  1. Products/List
  2. News/List

where "list" view Inherits System.Web.Mvc.ViewPage<Model<BaseList>>

controller renders view i.e View("List", ProductsList)


edited

return View("List", new Model<ProductsList>());
Model<T> where T : IMyList 
ProductsList : BaseList
NewsList : BaseList
BaseList : IMyList


where ProductsList & NewsList : BaseList

compiles fine but get a runtime error about differing model types.

If this not possible what is the best way to accomplish this rather than creating n no of views ?

A: 

Your question is a little unclear, but I guess it has to do with you having an IList<Base> in your model and you want to pass an IList<Derived>. This is not possible, but in C#4 you can pass an IEnumerable<Derived> instead of an IEnumerable<Base>.

erikkallen
+1  A: 

I think (but am not sure) this has to do with Covariance.

How I'm doing this is create a strongly typed viewData like for example this:

public class YourViewData: System.Web.Mvc.ViewDataDictionary
{
    public IMyList TheList { get; set; }
}

You can then make a ViewPage that you call like

return View("List", new YourViewData(){TheList = new ProductList(){abunchOfItems});

Disclaimer: All freehand code

borisCallens