I've found many similar questions but nothing with quite the answer I'm after. Part of my problem is due to the inability to use Generics in Attributes.
I am probably trying to over-complicate things, so if you can think of an easier way of doing this I'm all ears.
My specific problem relates to ASP.NET MVC, using Attributes (Filters) on an Action method. I'm trying to create a filter that will paginate the results passed to the ViewData.Model like this:
[PagedList(PageSize = 2, ListType = typeof(Invoice))]
public ViewResult List()
{
var invoices = invoicesRepository.Invoices; // Returns an IQueryable<Invoice>
return View(Invoices);
}
My filter's OnActionExecuted override then looks like:
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
ViewResult result = (ViewResult)filterContext.Result;
var list = (IQueryable<?>)result.ViewData.Model; // Here I want to use the ListType in place of the ?
// Perform pagination
result.ViewData.Model = list.Skip((Page - 1) * PageSize).Take(PageSize.ToList();
}
I realise I could replace my
var list = (IQueryable<?>)result.ViewData.Model;
With
var list = (IQueryable)result.ViewData.Model;
IQueryable<Object> oList = list.Cast<Object>();
But my view is strongly typed to expect an
IQueryable<Invoice>
not an
IQueryable<Object>