Receive generic IPageable in Razor View

Dec 27, 2011 at 4:18 PM


I'm trying to receive a model of type IPageable in my view, but I can't get it to work. I have some classes which inherits from one specific one (let's say, "Entity"). I thought I should be able to have a View which declares "@model IPageable<Entity>" and I could be able to receive any of the inherited classes (just like I can do with IEnumerable<Entity>, for example). But now I see it's not that simple. If I try to do this, I get this Exception:

The model item passed into the dictionary is of type 'Omu.Awesome.Core.Pageable`1[ProjectXYZ.Core.Model.Client]', but this dictionary requires a model item of type 'Omu.Awesome.Core.IPageable`1[ProjectXYZ.Core.Model.Entity]'.

I have tried with the View expecting either Entity or IEntity, but none seemed to work. Is there any workaround for this? Is this, in fact, an Awesome's problem? (I believe it is beacuse the same piece of code works if I replace "IPageable" for "IEnumerable", but.. who knows...)

Thank you! 

Dec 27, 2011 at 8:03 PM

which version of .net are you using ?

Dec 27, 2011 at 9:07 PM

.NET 4, MVC 3.

Dec 27, 2011 at 9:22 PM

Just to make sure everything's clear, I'll post a small example of my structure:

public class Entity {
    public int Id { get; set; }

public class Client : Entity {
    public string Name { get; set; }

public class GenericController<TEntity> : BaseController
    where TEntity : Entity, new()

    public ActionResult Index(int? page)
        var data = dbSet<Client>().All;
        var pageIndex = page ?? 1;
        return View(new Pageable<TEntity>
            PageIndex = pageIndex,
            PageCount = GetPageCount(pageSize, data.Count()),
            Page = data.Skip(--pageIndex * pageSize).Take(pageSize)

    static int GetPageCount(int pageSize, int count)
        var pages = count / pageSize;
        if (count % pageSize > 0) pages++;
        return pages;


And my view:

@model IPageable<Entity>

@foreach (var item in Model.Page) {
	<p>ID: @item.Id</p>



ps: didn't test this simple case, but should work.

Dec 27, 2011 at 9:30 PM
Edited Dec 27, 2011 at 9:32 PM

well, I guess this cast is not gonna work,

btw, didn't knew that there is anyone using the Pager, that's the first ever question about it

Dec 27, 2011 at 9:36 PM

I think it has something to do with covariance and contravariance, and for some reason this doesn't work here

in .net 3.5 this shouldn't work for sure